Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In MS C# Unit Testing, how to Assert that an ArgumentException occured?

If we do a

throw new ArgumentException("Cannot do that");

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

like image 925
Zachary Scott Avatar asked Jul 13 '10 17:07

Zachary Scott


1 Answers

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 :-)

like image 116
Darin Dimitrov Avatar answered Oct 23 '22 13:10

Darin Dimitrov