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.
- Using Visual Studio 2012 Ultimate, create a new C# Class Library project
- Add a new unit test project to the solution
- Add a reference to the class library to the test project
- Right-click the test project's System reference - the context menu appears
- In the context menu, select "Add Fakes Assembly" - the Fakes folder is added to the project, containing two files:
- mscorlib.fakes
- System.fakes
- Open the mscorlib.fakes file - the file should contain this XML code:
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.
"Add the following element" - the fakes xml isn't htmlencoded, so it's getting eaten by my browser
ReplyDeleteHere is the missing text for the element you add to mscorlib.fakes:
ReplyDelete<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>
...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.
ReplyDelete<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
<Assembly Name="mscorlib" Version="4.0.0.0"/>
<ShimGeneration>
<Add FullName="System.Environment"/>
</ShimGeneration>
</Fakes>
Thanks. It will be good to include the xml in the body of post instead of keep it in comments
ReplyDeleteThere 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".
ReplyDeleteHowever note that examples have missing version in
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