Testing for exceptions in MSpec
Posted by Jens Pettersson on September 10th, 2010As I’ve said before, we’re using MSpec in our current project and today I needed to make sure a method threw the right kind of exception. This may be really easy, but as I’ve never done this in MSpec before, I had to ask the mighty Google for the answer. When ever I have to do that, I want to write it down somewhere (but I seldom do), and that’s why I’m posting this here.
Consider we have the following, very useful, method in a Very Fragile class:
Now we want to make sure that when the bomb is dropped our calling code gets a Potential Dangerous Exception. The following specification will test that this really happens:
That’s it! Using the Catch-class in MSpec will return an Exception and then all you have to do is check that it is of the correct type.
Happy MSpec-ing!
//J
PS. If you wonder why I’m not using our syntax highlighting plugin the simple answer is that I love the Visual Studio theme I’m using and think Daniel should switch to it now!
Recent Comments