There is one great question that arises in any discussion related to unit testing. "Do I need to create tests for unit tests". The answer to this question, as a rule, is Code Coverage technology. Indeed, if you want to make sure that the unit test is prepared correctly, you only need to check whether all the branches in the code are called. This is achieved by a simple method - it is necessary to submit to the input of the checked function all combinations of data that will allow to bypass these branches. And academic examples from the documentation show this.
But the catch is that the real world is more complicated. Application functions can take into account the conditions not only supplied to the input. How to be in this case?
Once again about Code Coverage.
Imagine that you need to check the code that carries out the purchase of goods:

')
If you create a unit test in this case, it is obvious that you need to provide for calling this function with various parameters. For the case itemID = 0 and ItemID <> 0. In this version, all branches of the code will be called, and this unit test will have a Code Coverage value of 100%.

Complicate the task
A more complex code close to reality may have the following form:

How to be in this case? Inside the function, the CLR Date.Time call is used, and we have no influence on the return of the data of this function. No matter how hard we try to complicate the logic of the unit test, the function of the function depends on
external conditions . What, now run the unit tests on a schedule twice a day, in the afternoon and in the evening to check all the code branches?
You can of course use the Visitor pattern. When creating a class with the PurchaseItem method, we will pass a certain IGetCurrentTime interface that, when the program runs, will have one implementation, and when the unit of the test is called, it will be replaced. Thereby we will achieve
isolation from external conditions . This is a good option, which is generally recommended by everyone. But are there any other options?
We isolate ourselves from the outside world with the help of Moles
For Visual Studio 2010 there is a wonderful addition Moles. This is the so-called Isolation Framework which can help us in such cases:

You can install it from the Extension Manager, as they say, without leaving Visual Studio. You can start exploring its capabilities here at
research.microsoft.com/en-us/projects/pex/documentation.aspx , as well as on Habré there are several interesting and detailed articles.
The capabilities of this add-on are quite broad, including allowing you to create “interceptors” of the functions of your application or any other external function. In this case, you no longer need to change the source code of your program.
Intercepting DateTime.Now
So, what should be done so that our unit test does not depend on the returned values ​​of DateTime.Now?
The first step is to add to our Moled Assembly test project - a specially prepared stub for the MSCorLib assembly, which contains the DateTime.Now method

The second step is to declare that we are really going to intercept the DateTime.Now method:

The third step is actually rework our unit tests:

Some explanations: The HostType attribute is required in order to indicate to the Moles Runtime subsystem that this unit test will intercept external functions. Further, the code indicates that the DateTime.Now method should actually be taken from the lambda. At the beginning it was stated that Moles generated a special stub for mscore. The types in this stub have prefixes and suffixes. For example, for DateTime.Now, the stub would be System.
Moles.M DateTIme.Now
Get . By running this unit tests, we again get the Code Coverage value of 100% regardless of what the current time is.
Conclusion
Of course, this example is very simple, and Moles is capable of more complex use cases. The actual application of this technology can be convenient for isolating database calls, a file system, a complex API associated with hardware, web services from external providers, and many other scenarios. But nevertheless, this example will help you to be sure that you can write a unit test to any even the most complex function.
By the way, tomorrow at the Microsoft office in Krylatskiye Hills, the Microsoft Quality Assurance Day
www.microsoft.com/ru-ru/events/msqadays/index.html event will be held, which will discuss software quality assurance. In my report will be mentioned Moles and I will show "live" how these mechanisms work. If you are interested in this technology, be sure to watch the broadcast.