When running this test:
[TestMethod]
[ExpectedException(typeof(SpecialException))]
public void Test1()
{
throw new NotImplementedException();
}
Visual Studio tells me why it fails:
Test method [...].Test1 threw exception System.NotImplementedException, but exception [...].SpecialException was expected. Exception message: System.NotImplementedException: The method or operation is not implemented.
But when I try to extend ExpectedExceptionBaseAttribute
(to expect for error code in SpecialException
) like that:
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
class ExpectedSpecialException : ExpectedExceptionBaseAttribute
{
protected override void Verify(Exception exception)
{
SpecialException se = exception as SpecialException;
if (se == null)
{
RethrowIfAssertException(exception);
throw new Exception("SpecialException was expected but test method threw another one");
}
}
}
and use it like that:
[TestMethod]
[ExpectedSpecialException]
public void Test1()
{
throw new NotImplementedException();
}
VS produces a not very informative message:
Exception has been thrown by the target of an invocation.
All examples (1,2,3) of extending ExpectedExceptionBaseAttribute
I found on the internet have the same attempt to provide more info about failure in the thrown exception, but with no result.
Am I doing something wrong? Is there's a way to provide more information about such failed tests?