I’m using NUnit and Microsoft Code Contracts library to implement unit tests and code contracts (mostly preconditions).
Unfortunately Contracts library throws a generated (internal) exception class that can’t be used as Assert.Throws<ContractException>(…).
Contracts documentation suggests the following code for MSTest framework:
I wrote the following code to mimic this functionality:
Well, it did not work, I couldn’t make the Assert.Throws structure work. I always got the ContractException exception. Finally I found a blog entry (in German) with a classic solution:
- use try/catch block
- check exception type name
So here is the working code: