2017-08-01 7 views
1

私はTDD開発の新人です.Nunit 3.7.1、C#、および.NET Framework 4.7でいくつかのテストを始めました。個人的なnunitテストを使って自分自身を繰り返さない

私は、このテストクラスがあります。このクラスをテストする

[TestFixture] 
class ProcessTrzlExportTest 
{ 
    private ImportTrzlBatch _import; 

    [SetUp] 
    public void SetUpImportTrzlBatch() 
    { 
     _import = new ImportTrzlBatch(); 
    } 

    [Test] 
    public void ShouldThrowArgumentExceptionWithNullPath() 
    { 
     // Arrange 
     string path = null; 

     // Act 
     ActualValueDelegate<object> testDelegate =() => _import.LoadBatchFile(path); 

     // Assert 
     Assert.That(testDelegate, Throws.TypeOf<ArgumentNullException>()); 
    } 

    [Test] 
    public void ShouldThrowArgumentExceptionWithEmptyPath() 
    { 
     string path = string.Empty; 

     // Act 
     ActualValueDelegate<object> testDelegate =() => _import.LoadBatchFile(path); 

     // Assert 
     Assert.That(testDelegate, Throws.TypeOf<ArgumentNullException>()); 
    } 

    [Test] 
    public void ShouldThrowArgumentExceptionWithWhiteSpacesPath() 
    { 
     string path = " "; 

     // Act 
     ActualValueDelegate<object> testDelegate =() => _import.LoadBatchFile(path); 

     // Assert 
     Assert.That(testDelegate, Throws.TypeOf<ArgumentNullException>()); 
    } 
} 

public class ImportTrzlBatch 
{ 
    public object LoadBatchFile(string path) 
    { 
     if (string.IsNullOrWhiteSpace(path)) 
      throw new ArgumentNullException(nameof(path)); 

     return null; 
    } 
} 

私はpathは、ヌル空または空白でないことをテストしています。 3つの異なる入力を使って同じメソッドをテストする3つのテストメソッドがあります。

プライベートテスト方法を使用して、コードを繰り返し実行せずに、これらの3つのメソッドから3つの異なるパスを呼び出すことはできますか?それはArgumentNullExceptionを投げるかどうかをテストするには
ActualValueDelegate<object> testDelegate =() => _import.LoadBatchFile(path);


もう一つの問題は、私はこれを使用しているということです。

デリゲートを使用せずに例外をスローするかどうかをテストする別の方法はありますか?ところで

は、私はそれからArgumentNullExceptionを投げるかどうかを確認するためのコードをコピーして、このSO答え:私はテストケース(下記参照)を使用する傾向があり、これらのシナリオではhttps://stackoverflow.com/a/33897450/68571

+0

あなたは 'Assert.Throwsを使用することを意味するか <>()'や 'Assert.DoesNotThrow <>()'? – kayess

+0

私は分かりません。私は、このSOの答えから 'Assert.That'をコピーしました:https://stackoverflow.com/a/33897450/68571 – VansFannel

+0

同じ投稿で2つの質問をするべきではありません。 –

答えて

1

TestCaseSourceAttributeを使用することをお勧めします。IEnumerableを実装する1つのオブジェクトにすべての入力ケースを入れて、このオブジェクトのテストを呼び出すことができます。だから、次のようになります。

[TestCaseSource(nameof(ShouldThrowArgumentSource))] 
    public void ShouldThrowArgumentException(string path) 
    { 
     // Act 
     ActualValueDelegate<object> testDelegate =() => _import.LoadBatchFile(path); 

     // Assert 
     Assert.That(testDelegate, Throws.TypeOf<ArgumentNullException>()); 
    } 

    private static IEnumerable ShouldThrowArgumentSource() 
    { 
     yield return string.Empty; 
     yield return null; 
    } 

詳細については、あなたがdocumentationを読むことができる程度TestCaseSource

4

。このように、テストは一度だけ作成し、テストするさまざまな値を指定します。あなたのケースではnullと空の文字列。

[TestCase(null)] 
[TestCase("")] 
public void ShouldThrowArgumentException(string path) 
{ 
    // Act 
    ActualValueDelegate<object> testDelegate =() => _import.LoadBatchFile(path); 

    // Assert 
    Assert.That(testDelegate, Throws.TypeOf<ArgumentNullException>()); 
} 

そして、あなたはちょうどこのようにデリゲートをインライン化することができます:TestCaseクラスの機能の詳細については

[TestCase(null)] 
[TestCase("")] 
public void ShouldThrowArgumentException(string path) 
{ 
    Assert.That(() => _import.LoadBatchFile(path), Throws.TypeOf<ArgumentNullException>()); 
} 

では、このマニュアルを見てhere

0

テストコードは、他のコードのようなものです。何かを抽象化することが理にかなっている場合は、それを行います。あなたのケースでは、あなたはあまり勝ちません。私はCedric Royer-Bertrandの解決策が気の利いたと思います。追加するだけで

[TestCase(" ")] 

となります。

関連する問題