In the past I’ve used:

[ExpectedException(typeof(CustomException), ExpectedMessage="blah blah blah")]

to set an exception expectation and assert on the message. With the recent releases of NUnit I’ve finally transitioned all my code to use:

Assert.Throws<CustomException>(() => someType.someMethod(someValue));

or

Assert.Throws<CustomException>(someType.someMethod);

if the method has no parameters.

Only lately have I needed to capture the exception message and validate it. To do this you do the following:

var ex = Assert.Throws<CustomException>(() => someType.someMethod(someValue));

Assert.That(ex.Message, Is.EqualTo("expected message here"));

I am blogging about this because I keep forgetting how to capture the exception message. Hope it helps someone else.