C# Unit Test Getting Started with Complete Code

This article is a supplement of the basic introduction of C# Unit Test from MSDN. As you may find, although technically correct, the MSDN article is confusing here and there, probably because it was written by several people with different assumptions. Another defect of the MSDN article is that the code samples are either too much – so it cannot focus on the point, or too little – so it loses its connection with other essential parts of a complete Unit Test solution. That’s why I am writing this article, with complete code sample tested in VS2013 that you can download.

There are three Sections of this article, with three corresponding VS solutions in the code sample: Basic Unit Test, Unit Test with Stub, and Unit Test with Shim. You should follow the order if you are new to this topic.

Basic Unit Test

Please read the MSDN Unit Test Basics article first, EXCEPT the “Additional tools” section. And refer the “UnitTest” sample in my code.

Note that there are only two projects in the VS solution: Accounts project with only one code class CheckingAccount.cs, and AccountsTest project with only one test class CheckingAccountTest.cs. The mention of BankDB and BankDBTest projects, as well as other interfaces or classes in the MSDN article should all be ignored for now.

Now it should be easy to get the point of Unit Test.

Unit Test with Stub

Now go back to read the “Additional tools” section of the MSDN Unit Test Basics article, esp. the last part “Isolating unit test methods with Microsoft Fakes”. Does it make much sense? I would not be suprised if you think it doesn’t. In fact, there are even errors in the code samples in this part: “var stubBankDb = new MyBank.Stubs.StubIBankDb();” is from an older version of Microsoft Fakes (called Microsoft Moles, just in case you are wondering). In VS2013 it should be “var stubBankDb = new MyBank.Fakes.StubIBankDb();”. Now you understand what I was talking about at the beginning of this article.

OK. Now go ahead and read the MSDN Microsoft Fakes article. This one should make much more sense. And refer to the “UnitTestWithStub” sample in my code.

The addition of a new project BankDB represents a different component that introduces other dependencies, like network connection, database connection, certian files that should be available in the client’s computer on runtime, etc. This is where Stubs are supposed to work. Note that the code samples in the MSDN Fakes article are no longer the Bank/Account ones, while I am still keeping the same context in my code sample.

If you try to follow the article by clicking “Add Fakes Assembly”, you will find the following error message. Deleting the reference is not enough, you may need to delete the Fakes\BankDb.fakes file as well.

7-16-2015 3-12-39 PM

Unit Test with Shim 

The Shim sample code should be pretty straight forward. It’s basically highjacking a call to an external reference and return a preset value.

One problem is that VS2013 Shim generation with System reference seems to be not working well with .net Framework 4.5(.x), with a strange error regarding System.Security.Cryptography. Changing the target framework to 4.6 would work. Or, try VS2015.