Doing the Shimmy Fake (VS11 Beta Unit Test Isolation).

I’m sure many of you have read of or heard about the Moles Power Tool that shipped as an add-on to the original Visual Studio 2010 release back in 2009. There was certainly a lot of speculation about which way this Microsoft Research project would go.

The great news is that with the impending Visual Studio “11” release it appears the Moles Power Tool is now baked into the Microsoft.QualityTools namespace.

I first read about this on the MSDN ALM blog which I’d recommend you go and have a look at. My first thought was that the new Shim Fake is essentially what a Mole was previously. In writing up a few simple test cases my hunch was confirmed (see the tooltip reads “Sets the mole of…”:


(I expect this will change before the RTM release though!)

So what can you do with a Shim? A Shim allows you to redirect calls to a method to a delegate you control which means you can control flow in tests that might otherwise have been blocked. Where I’ve used this heavily previously was with SharePoint 2010 classes and with the System.Configuration namespace.

You need to do a bit of work to create the baseline for a Fake – firstly the assemblies must be referenced in your Unit Test project in Visual Studio – right click on the assembly under “References” and choose “Add Fake Assembly”:

The Add Fakes Assembly context menu item.

Visual Studio will add some XML files that define the Fake and a new reference will appear that represents the Fakes:

Solution Explorer with Fakes.

A Simple Example

The easiest way to show how this all works in your favour is with a simple example. Firstly let’s assume we have a simple helper class that utilises System.IO.File. In the past we would have had to resort to 3rd party products to redirect any calls to System.IO.File so that we can test the code around the call. Here’s our File helper class definition:

public class FileWriter
     public void AppendContentsToFile(string filePath, string conentToAppend)
             System.IO.File.AppendAllText(filePath, conentToAppend);
         catch (Exception ex)
             // log and throw.

And now let’s see how we use a Shim to allow us to test the FileWriter AppendContentsToFile method. The important elements to note are: using the Fakes namespace so that you can create a ShimContext which is needed to use a Shim. You’ll also notice that the Shim has a different class name and that the method combines the name and type of each parameter. If you don’t use a ShimContext your test will fail, but helpfully the test runner will actually provide a code sample of what needs doing in order to fix the test.

By using an anonymous delegate we provide a redirection of the call to a method we write and control. This quickly becomes a powerful way to isolate just your code from underlying Framework methods.

using Microsoft.QualityTools.Testing.Fakes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UnitTestingDemoApp;

namespace UnitTestProject1
    public class UnitTest1
        public void TestFileWriterAppendContents()
            using (ShimsContext.Create())
                var writer = new FileWriter();

                System.IO.Fakes.ShimFile.AppendAllTextStringString = (file, contents) =>; AppendStringToFile(file, contents);

                writer.AppendContentsToFile("C:\\some\\path\\afile.text", "somecontents");

        private void AppendStringToFile(string filePath, string textToAppend)
            // perform some logic, maybe throw exceptions.

This is a simple example of how Shims can be used – right now beyond MSDN there isn’t a lot of detail about but if you read up on Moles you will quickly get the idea.

And the Shimmy Fake? Well, I tip my hat to Australian kids entertainers the Wiggles and their Shimmy Shake from 2008…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s