How do I test for exceptions in a TestCase with NUnit3?
Let's say I have a method Divide(a,b)
defined as follows:
public double Divide(double a, double b)
{
if(Math.Abs(b) < double.Epsilon) throw new ArgumentException("Divider cannot be 0");
return a/b;
}
I want to test this method using NUnit 3.0 test cases, so maybe I have:
[TestCase(-10, 2, -5)]
[TestCase(-1, 2, -0.5)]
public void TestDivide(double a, double b, double result)
{
Assert.That(_uut.Divide(a, b), Is.EqualTo(result));
}
Is there a way to specify a Test Case that will cause Divide() to throw an ArgumentException and somehow have this as the expected result, e.g. something along the lines of:
[TestCase(-10, 2, -5)]
[TestCase(-1, 2, -0.5)]
[TestCase(-1, 0, ExpectedResult = TypeOf(ArgumentException)]
public void TestDivide(double a, double b, double result)
{
Assert.That(_uut.Divide(a, b), Is.EqualTo(result));
}
(Of course I could define a separate test method and use Assert.Throws()
in this, so this is purely out of curiosity)