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 NUnit, I would avoid ExpectedException and use Assert.Throws (Exception Asserts) instead. It gives you finer-grained control. Otherwise the test will pass if any portion of the test method throws that exception.
In MSTest, you could get the same level of control with the old-school construct:
With this, there’s no need for the ExpectedException attribute.
(The concept comes from the book Test-driven Development: A Practical Guide by David Astels.)