English 中文(简体)
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 tests 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.





相关问题
Best practices for Subversion and Visual Studio projects

I ve recently started working on various C# projects in Visual Studio as part of a plan for a large scale system that will be used to replace our current system that s built from a cobbling-together ...

Enable PHP highlights in VS 2010?

I ve heard that Visual Studio 2010 Beta 2 has support for PHP. When I load a PHP file though, it has nothing highlighted and is nothing more than a glorified text editor. Is there a way to enable it?...

Do I want Publish or Release Build in VB.net?

I wrote a little (like 40 lines little) Winforms application with VB.net in Visual Studio 2010. Now I ve released the code as a Google Code project. It s easy for a developer to get the source but I d ...

Unit Testing .NET 3.5 projects using MStest in VS2010

There s a bug/feature in Visual Studio 2010 where you can t create a unit test project with the 2.0 CLR. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483891&wa=...

热门标签