none
Cannot find a shim for System.Environment.MachineName() etc RRS feed

  • Question

  • I am using shims in my project to do unit testing. I have a method which returns the machine name using System.Environment.MachineName(). I want to shim the MachineName() method to return a constant value for my unit testing but I am not able to find the method. Please advise.
    • Moved by Pradeep Narayan [MSFT] Monday, September 24, 2012 10:03 PM Moving to unit testing forum (From:Testing with Visual Studio Test Manager (MTM))
    Monday, September 24, 2012 8:29 PM

Answers

  • Try this:

    • In Solution Explorer expand your unit test project's references
    • In references right click on System
    • Select Add Fakes Assembly
    • In Solution Explorer expand your unit test project's Fakes
    • Select mscorlib

    The last step above will open the .fakes configuration file for mscorlib.  It should look something like this...

    <Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
      <Assembly Name="mscorlib" Version="4.0.0.0"/>
    </Fakes>
    • Extend the configuration file by adding a ShimGeneration element for Environment...
    <Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
      <Assembly Name="mscorlib" Version="4.0.0.0"/>
      <ShimGeneration>
        <Add FullName="System.Environment"/>
      </ShimGeneration>
    </Fakes>
    • Rebuild your solution
    • Open the Object Browser
    • Expand mscorlib.4.0.0.0.Fakes
    • Expand System.Fakes

    Under System.Fakes you should see a ShimEnvironment entry

    Within System.Fakes.ShimEnvironment you should see MachineNameGet

    Now you should be able to use the shim...

    [TestMethod] public void TestMethod2() { using (ShimsContext.Create()) { System.Fakes.ShimEnvironment.MachineNameGet = () => "NOTREALNAME"; string name = System.Environment.MachineName; Assert.AreEqual<string>("NOTREALNAME", name); } }

    This worked for me, Your Milage May Vary; Use At Your Own Risk!


    Sunday, October 7, 2012 10:40 AM

All replies

  • Hi Soumya,

    I try to get other people help you, please wait some time.

    regards,


    Forrest Guo | MSDN Community Support | Feedback to manager

    Tuesday, September 25, 2012 1:36 PM
    Moderator
  • Try this:

    • In Solution Explorer expand your unit test project's references
    • In references right click on System
    • Select Add Fakes Assembly
    • In Solution Explorer expand your unit test project's Fakes
    • Select mscorlib

    The last step above will open the .fakes configuration file for mscorlib.  It should look something like this...

    <Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
      <Assembly Name="mscorlib" Version="4.0.0.0"/>
    </Fakes>
    • Extend the configuration file by adding a ShimGeneration element for Environment...
    <Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
      <Assembly Name="mscorlib" Version="4.0.0.0"/>
      <ShimGeneration>
        <Add FullName="System.Environment"/>
      </ShimGeneration>
    </Fakes>
    • Rebuild your solution
    • Open the Object Browser
    • Expand mscorlib.4.0.0.0.Fakes
    • Expand System.Fakes

    Under System.Fakes you should see a ShimEnvironment entry

    Within System.Fakes.ShimEnvironment you should see MachineNameGet

    Now you should be able to use the shim...

    [TestMethod] public void TestMethod2() { using (ShimsContext.Create()) { System.Fakes.ShimEnvironment.MachineNameGet = () => "NOTREALNAME"; string name = System.Environment.MachineName; Assert.AreEqual<string>("NOTREALNAME", name); } }

    This worked for me, Your Milage May Vary; Use At Your Own Risk!


    Sunday, October 7, 2012 10:40 AM