私は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
あなたは 'Assert.Throwsを使用することを意味するか <>()'や 'Assert.DoesNotThrow <>()'? – kayess
私は分かりません。私は、このSOの答えから 'Assert.That'をコピーしました:https://stackoverflow.com/a/33897450/68571 – VansFannel
同じ投稿で2つの質問をするべきではありません。 –