views:

20

answers:

1

If we do a

throw new ArgumentException("Cannot do that");

How do you Assert that this ArgumentException happened with Microsoft's Testing Framework?

+4  A: 

You could decorate your unit test with the [ExpectedException] attribute:

[ExpectedException(typeof(ArgumentException))]
[TestMethod]
public void Foo()
{
    throw new ArgumentException("foo");
}

Don't ask though about asserting the exception message :-)

Darin Dimitrov
Thus why Unit tests shouldn't test more than one thing. What if you did a Try / Catch then processed the caught exception with an Assert?
Dr. Zim
Well, of course you could do that but in this case you will be writing, let me count, 7 more lines of code (probably a bit less if you put the `{` on the same line), and every line of code you write is hiding a potential error and decreasing the readability of the code, so why doing it when you can avoid it :-)
Darin Dimitrov