17 October 2012

How to generate missing Fakes shims

While using the Fakes (Moles) isolation framework, you will notice some mscorlib shims are missing, particularly in the System namespace. For example, System.Fakes.ShimEnvironment does not exist. Although these are missing by default, you can request the compiler to generate shims for specific objects. This post provides steps to request Fakes shim generation for System.Environment and an example of implementation.

This tutorial targets Microsoft Fakes Isolation Framework on Visual Studio 2012 Ultimate or later; but, this process is also compatible with the developmental version, project name "Moles", running on Visual Studio 2008 or 2010. This tutorial assumes you have basic understanding of the MSTest framework.

How to generate Fakes shims


The process of requesting shim generation is simple. First, we'll create a project with tests.
  1. Using Visual Studio 2012 Ultimate, create a new C# Class Library project
  2. Add a new unit test project to the solution
  3. Add a reference to the class library to the test project
  4. Right-click the test project's System reference - the context menu appears
  5. In the context menu, select "Add Fakes Assembly" - the Fakes folder is added to the project, containing two files:
    1. mscorlib.fakes
    2. System.fakes
  6. Open the mscorlib.fakes file - the file should contain this XML code:


     


  • Add the following element, to request generation of the System.Environment shim:

     
       
         
       


  • Build the solution - this will take a little longer than usual, while the mscorlib Fakes assembly is compiled

  • Implementation

    To prove this shim has been created, we build a simple test. First, open the default Class1.cs file, in the class library project. Create the  following GetUserName method. The file should contain this code:

    Class1.cs

    namespace ClassLibrary1 {
      public static class Class1 {
        public static string GetUserName() {
          return System.Environment.UserName;
        }
      }
    }


    Next, open the default UnitTest1.cs unit test file, in the test project. Replace all default code with the following:

    UnitTest1.cs

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

    namespace ClassLibrary1.Test {

      [TestClass]
      public class Class1Test {

        [TestMethod]
        public void GetUserName_ReturnsSystemEnvironmentUserName() {
          string expected = "MyUserName";
          string actual;
          using (ShimsContext.Create()) {
            ShimEnvironment.UserNameGet = () => expected;
            actual = Class1.GetUserName();
          }
          Assert.AreEqual(expected, actual);
        }

      }
    }

    In the Test Explorer window, click the Run All button. The test should pass, indicating the shim functioned as expected.

    6 comments:

    1. "Add the following element" - the fakes xml isn't htmlencoded, so it's getting eaten by my browser

      ReplyDelete
    2. Here is the missing text for the element you add to mscorlib.fakes:

      <fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
       <assembly name="mscorlib" version="4.0.0.0">
        <shimgeneration>
          <add fullname="System.Environment">
         </add>
        </shimgeneration>
       </assembly>
      </fakes>

      ReplyDelete
    3. ...and here is the corrected version! The version above was taken from the page source and has incorrect capitalisation which causes Visual Studio builds to fail.

      <Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
       <Assembly Name="mscorlib" Version="4.0.0.0"/>
       <ShimGeneration>
        <Add FullName="System.Environment"/>
       </ShimGeneration>
      </Fakes>

      ReplyDelete
    4. Thanks. It will be good to include the xml in the body of post instead of keep it in comments

      ReplyDelete
    5. There are more options in https://msdn.microsoft.com/en-us/library/hh708916.aspx?f=255&MSPPError=-2147217396 "Code generation, compilation, and naming conventions in Microsoft Fakes".
      However note that examples have missing version in

      ReplyDelete
    6. It should be noted that whilst ordering papers for sale at paper writing service, you can get unkind attitude. In case you feel that the bureau is trying to cheat you, don't buy term paper from it. investire alle canarie con rendita garantita

      ReplyDelete

    Please provide details, when posting technical comments. If you find an error in sample code or have found bad information/misinformation in a post, please e-mail me details, so I can make corrections as quickly as possible.