XUnit: testing for exceptions

I think I’ve written about this before… but I can’t find it. Basically I find the paradigm for exception testing nicer in XUnit compared with MsTest, in my view. Note that there is no decoration, and you can test easily both for the exception type, and for a message (in this case) that accompanies it:

[Fact]
public void RequestForNotFoundFrequencyThrowsException() {
TraceExecutingMethod();
var toneSet = new Model.ToneUtility();
Exception ex = Assert.Throws<ArgumentException>(() => toneSet.GetNoteElements(9999));
Assert.Equal("[The frequency [9999] was not found in the set of available notes]", ex.Message);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s