I'm new in TDD developing and I've just started to do some tests with Nunit 3.7.1, C# and .NET Framework 4.7.
I have this test class:
[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>());
}
}
To test this class:
public class ImportTrzlBatch
{
public object LoadBatchFile(string path)
{
if (string.IsNullOrWhiteSpace(path))
throw new ArgumentNullException(nameof(path));
return null;
}
}
I'm testing that path
is not null, empty or white space. I have three test methods to test the same method with three different inputs.
Can I use a private test method to not repeat the code and call it from those three methods with the three different paths?
Another question is that I'm using this:
ActualValueDelegate<object> testDelegate = () => _import.LoadBatchFile(path);
To test if it is throw an ArgumentNullException
.
Is there another way to test if throw the exception without using the delegate?
By the way, I have copied the code to check if it throws ArgumentNullException
from this SO answer: https://stackoverflow.com/a/33897450/68571