开发者

Unit Testing interface contracts in C#

Using the Code Contracts tools available in VS2010 Beta 2, I have defined an interface, a contract class for that interface and two classes that implement the interface.

Now when I come to test the code, I want to test the implementation classes so I know that their functionality is correct and I want to test the contract code so I know that my conditions are correct.

I could test each contract statement in each of the 2 implementation classes, but that is clearly redundant. I could just write the tes开发者_开发知识库ts on one of these implementation classes, but that seems a bit wrong, how to choose between them, remembering which one to update as you change the contracts etc.

I'd like to test the actual interface contract class, but get all sorts of compile time warnings that the interface method I want to test is not available on the interface contract class. I know that there is post-compilation magic happening to actually inject the contract code into my implementation classes (which I can see in ILDASM), but when I inspect the interface contract class methods, they are there in the MISL, but empty.

I am missing something, or is what I want to do just not possible. If it is not, what is the "best practice" for this?

===Edit===

One suggestion here is to implement the interface in a class (internal to the test assembly) whose purpose is solely to test the interface contracts, sound sensible?


Testing your contracts is like unit testing your unit tests. You don't do it because then you'd have to test the tests for your tests, etcetera.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜