I usually use Nunit but in my current project I am using MSTest. Now I have a test which expects an exception but keeps failing, and I do not have any idea as to why.
Here is a simple example I have used to replicate the problem:
[TestMethod, ExpectedException(typeof(ErrorResponseException))]
public void should_throw_exception()
{
throw new ErrorResponseException();
}
The ErrorResponseException is a class that just inherits from Exception, that is it, anyone know why this is failing, I would expect it to pass.
In Visual Studio 15
with dependencies on MSTest.TestAdapter v1.1.18
and MSTest.TestFramework v1.1.18
you could also use
Assert.ThrowsException<ArgumentNullException>(() => MethodThatThrowsArgumentNullException());
Shameless plug, but I wrote a simple assembly that makes asserting for exceptions and exception messages a little easier and more readable in MSTest using Assert.Throws() syntax. I wrote a blog post with the full details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With