I am trying to work with the ExpectedException
attribute in a C# UnitTest
, but I am having issues getting it to work with my particular Exception
It will fail unless the type of exception is exactly the type you've specified in the attribute e.g
PASS:-
[TestMethod()]
[ExpectedException(typeof(System.DivideByZeroException))]
public void DivideTest()
{
int numerator = 4;
int denominator = 0;
int actual = numerator / denominator;
}
FAIL:-
[TestMethod()]
[ExpectedException(typeof(System.Exception))]
public void DivideTest()
{
int numerator = 4;
int denominator = 0;
int actual = numerator / denominator;
}
However this will pass ...
[TestMethod()]
[ExpectedException(typeof(System.Exception), AllowDerivedTypes=true)]
public void DivideTest()
{
int numerator = 4;
int denominator = 0;
int actual = numerator / denominator;
}