Monday, July 13, 2009

How to check exception message in NUnit 2.5

In previous post, I mentioned how to check for expected exceptions in NUnit 2.5 to run with ReSharper and my prefered way but it lacks of how to check for exception messages. In this post, I will address it.

To check exception messages, I cannot use Assert.That(), my prefered way, because it returns nothing. The only way I can use is Assert.Throws(). Assert.Throws() returns an instance of the exception, and using it, I can check the message.

ArgumentException ex = Assert.Throws<ArgumentException>(delegate { clz.Divide(3, 0); });
Assert.AreEqual("Hey man, you need to learn elementary school again!", ex.Message);

Happy testing! :-)

No comments: