Thursday, November 14, 2013

Migrating from Moles to Fakes

If you have used Microsoft Moles for developing your Unit Tests with Visual Studio 2010, as many of you already know, you are out of luck when it comes to using Visual Studio 2012 or Visual Studio 2013 since Moles is no longer supported on either of these IDEs.

Instead, if you have Visual Studio 2012 Ultimate Edition or Visual Studio 2013 Premium Edition and above, you can migrate to the use of the Fakes Framework.

Of course, migration to the Fakes Framework is not an automated migration process.  In fact, the code has to be manually migrated through search and replace and a significant amount of editing.  There are a great deal of articles that provide a great deal of detail on how to migrate, but this is the "For Dummies" version if you will:


  1. Open up your solution in Visual Studio 2012/2013
  2. For all of the broken references that refer to Moles Assemblies, right-click on the original assemblies and select "Add Fakes Assembly"
  3. Remove all of the broken references to the Moles Assemblies from the References area of each Unit Test project
  4. You can also remove all of the .moles files from each of your Visual Studio projects.
  5. Now, comes the very manual and very tedious part of replacement.
    • For every using statement that includes the extension .Moles, replace it with .Fakes
    • For every assembly that begins with "M", replace it with Shim
    • For ever assembly that begins with "S", replace it with Stub
    • Remove all instances of the [HostType("Moles")] attribute from all Test Methods
    • In addition, for every use of a Shim, you will have to surround the code with a using statement: using (ShimsContext.Create()) { }
  6. Now, make sure that all of your projects in the solution compile.
  7. If you are able to successfully compile your solution, you can now proceed to run all of your Unit Tests.
  8. Verify that all of your Unit Tests pass after the migration to Fakes.



No comments:

Post a Comment