I first met with unit testing in Java and was pleased to be able to do moki on final classes, on static members. At that time, on .Net, nothing like this could be done. Only interfaces. You can infinitely long to argue that if you needed to do something unnatural, then you need to rewrite the implementation, do IOC, and so on. But when it comes to inherited code, unsuitable architecture for unit testing, the ability to change things without rewriting helps out.
I finally abandoned Java and went to .Net and every time I talked about unit tests, I remembered that there were more Java possibilities.
And in 2012 the studios added the ability to make any kind of objects. Absolutely any, even systemic. Under the cut, translation of an article from MSDN (translated only by Shima, since stubs are of no particular interest).
Microsoft uses the term "shim", I will not try to translate it, let it be - we will.What is shimaMicrosoft Fakes helps isolate the tested code in two ways - shims and stubs.
The Shims modify the compiled code in runtime, so the method call is replaced by a call to the code you wrote. Shims can be used to substitute calls to assemblies that you cannot control, such as .Net Framework assemblies.
The choice between stubs and shimaUsually, a project in VS is developed as a separate component and there is a desire to use stubs or shims for used objects from other projects in a solution or external assemblies to which the project refers.
Try to use stubs for the code that is inside the solishin, and shims for external assemblies. It is considered good practice to decompose elements, highlighting interfaces, which allows stubs to be used, but external assemblies such as System.dll usually do not have this decomposition, so you must use shima.
')
UsingImagine that your component has a call to DateTime.Now:
// Code under test: public int GetTheCurrentYear() { return DateTime.Now.Year; }
During the test, you want to make PWM Now, because the original always returns different values
To use shim you do not need to modify the code or write it in a certain way.
1. Adding fake buildIn Solution Explorer, open the references of your project and select the assembly in which the classes you want to replace are located. The DateTime class is in the System.dll assembly. and select “Add Fakes Assembly” in the context menu.
2. Using shims inside ShimsContext [TestClass] public class TestClass1 { [TestMethod] public void TestCurrentYear() { int fixedYear = 2000; // Shims can be used only in a ShimsContext: using (ShimsContext.Create()) { // Arrange: // Shim DateTime.Now to return a fixed date: System.Fakes.ShimDateTime.NowGet = () => { return new DateTime(fixedYear, 1, 1); }; // Instantiate the component under test: var componentUnderTest = new MyComponent(); // Act: int year = componentUnderTest.GetTheCurrentYear(); // Assert: // This will always be true if the component is working: Assert.AreEqual(fixedYear, year); } } }
Names of the classes begin with the prefix Fakes.Shim, added to the original names.
The example shim is used for the static method, to use the shima for a non-static method, add AllInstances between the type name and the method name:
System.IO.Fakes.ShimFile.AllInstances.ReadToEnd = ...
Shims can also be created for specific objects, constructors, properties.
If this translation turns out to be interesting to people, then I will translate the next article, where examples of using shims for all cases are given.