我是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
不为空,空或空白。我有三种测试方法来测试三种不同输入的相同方法。
我可以使用私人测试方法不重复代码,并从这三种方法调用它与三个不同的路径?
另一个问题是,我使用的是这样的:
ActualValueDelegate<object> testDelegate =() => _import.LoadBatchFile(path);
为了测试是否是抛出一个ArgumentNullException
。
是否有另一种方法来测试是否抛出异常而不使用委托?
顺便说一句,我抄代码来检查它是否抛出ArgumentNullException
从这个苏答案:https://stackoverflow.com/a/33897450/68571
你的意思是使用'Assert.Throws <>()'或'Assert.DoesNotThrow <>()'? – kayess
我不知道。我已经从这个SO复制了'Assert.That'回答:https://stackoverflow.com/a/33897450/68571 – VansFannel
你不应该在同一篇文章中提出两个问题。 –