Share →
Buffer

Shims and stubs are part of the new Microsoft Fakes Framework.  I decided that I should go and learn about them, but just finding the most basic example to start the process was harder that I thought it would be.  Either people had examples and did not show how to implement them, or the examples were slightly more complicated than I wanted for a simple example.  Using a combination of the two, I was able to figure out how to do it, but I feel like there should have been a very simple example.  That is what I am going to show you today.  I will use the examples from the MSDN website on shims and stubs, but I will walk through the process of how to create the shims and stubs.

In order to make shims, in Visual Studio 2012 open a new project.

shims1

Under Installed > Templates > Visual C# pick a Class Library Project and name it Y2KBug.

shim2

Hit OK.  This will generate the project for you.  It will have an automatically generated class file named Class1.cs.  Rename the automatically generated class file to Y2KChecker.cs.

shim3

When the following message pops up, hit Yes.  This will make it so that everywhere in the file that says Class1.cs will then say Y2KChecker.cs instead.

shim4

Add the following code to your Y2KChecker.cs file.  This program will check to see if the date is January 1, 2000, and if it is, throw an error saying that it is the Y2K bug.

shims5

Next you are going to add a testing project to your solution file. Do this by right-clicking on Solution ‘Y2KBug’ under Solution Explorer and go Add > New Project.

shim6

Under Installed > Visual C# > Test pick Unit Test Project and name it Y2KBug.Test.

shim7

Hit OK.  This will add a testing project to your solution file.  It will have an automatically generated class file named UnitTestProject1.cs.  Rename the automatically generated class file to Y2KBugTest.cs.

shim8

When you get the same pop up as before, hit Yes.  In your References folder, under Y2KBug.Test, right-click and hit Add Reference…

shim9

Under Solution > Projects check the reference for Y2KBug and hit OK.

shim10

This will add a reference to Y2KBug so that you can test it.  In the References folder under Y2KBug.Test, right-click on the System Reference and hit Add Fakes Assembly.

shim11

This will add Microsoft.QualityTools.Testing.Fakes, mscorlib.4.0.0.0.Fakes and System.4.0.0.0.Fakes to the References folder and mscorlib.fakes and System.fakes to the Fakes folder that was added. This is because this reference uses two libraries.

shim12

Add the using statements System.Fakes and Microsoft.QualityTools.Testing.Fakes to the top of Y2KBugTest.cs.

shim13

Then modify Y2KBugTest.cs to look like the following.

shim14

You need the using statement to clean up the shims after they are used. If you do not use the using statement, the program will throw an error. Then using the shim, we set DateTime.Now to call January 1, 2000. This will not be the case outside this test because of our using statement. Now run the test.

shim15

And it passes.

Congratulations!  You have done your first test using shims!  Lets move onto stubs.

In Visual Studio 2012 open a new project.

shim1

Under Installed > Templates > Visual C# pick a Class Library Project and name it FileSystem.

stub1

Hit OK.  Rename the automatically generated class file to FileSystem.cs.  Add the following code to your FileSystem.cs file.  This program has a file system that implements an interface that reads in the file name.  It then has another method that checks to see if the file name is an empty string or not.

stub2

Next you are going to add a testing project to your solution file. Do this by right-clicking on Solution ‘FileSystem’ under Solution Explorer and go Add > New Project.

stub3

Under Installed > Visual C# > Test pick Unit Test Project and name it FileSystem.Test.

stub4

Hit OK.  Rename the automatically generated class file to FileSystemTest.cs.

In your References folder under FileSystem.Test, right-click and hit Add Reference…

Under Solution > Projects check the reference for File System and hit OK.

stub5

Under the references of FileSystem.Test, right-click on the FileSystem Reference and hit Add Fakes Assembly.

stub6

This will add Microsoft.QualityTools.Testing.Fakes and FileSystem.Fakes to the References folder and FileSystem.fakes to the Fakes folder that was added.

stub7

Add the using statement FileSystem.Fakes to the top of FileSystemTest.cs.

stub8

Then modify FileSystemTest.cs to look like the following.

stub9

This is stubbing the interface of the file system so that the file MyFile.txt will always have “hello world” in it.  Now run the test.

stub10

And it passes.

Congratulations! You have done your first testing using stubs.

Print Friendly
  • Martin Capodici

    The fakes seem a bit clunky to me. So you now have to maintain and update all the fake assemblies to keep them in sync with the real ones, and the extra compilation time required for this. I guess this is a last resort if you really can’t mock properly any other way.

    The nicer solution for your example is to create your own DateTime class with testing features, and use it everywhere (make the build or CI fail someone uses System.DateTime)

  • Abi Bellamkonda

    Would you end up writing your own System.dll, System.Web.dll then to make it testable? If you take that approach then you need to find ways to optimise lot of things. Entity Framework, Serialisation and all internals. I would avoid reinventing wheel.