locked
Switching from Moles to Fakes: MWindowsIdentity to ShimWindowsIdentity RRS feed

  • Question

  • I have a whole bunch of unit tests I'm trying to switch from Moles to Fakes.  I am just getting started and have run into my first road block.

    In Moles I was able to mole windows identity and override the property value for name like this:

    MWindowsIdentity.AllInstances.GetName = t1 => username;

    I am trying to switch this over to Fakes and have been able to create the System.Security.Principal.Fakes from mscorlib.  I am able to reference ShimWindowsIdentity.AllInstances, but it doesn't contain GetName (no intellisense and compile error).  What has changed from Moles to Fakes that doesn't allow me to override GetName?


    Friday, January 18, 2013 4:32 PM

Answers

  • I was able to run the following test successfully with Visual Studio 2012 Update 1.

    using (ShimsContext.Create())
    {
        ShimWindowsIdentity.AllInstances.GetName = (identity) => "Blah";
        Assert.AreEqual("Blah", WindowsIdentity.GetCurrent().Name);
    }
    

    Here is my .Fakes file:

    <Fakes xmlns="http://schemas.microsoft.com/fakes/2011/" Diagnostic="true">
      <Assembly Name="mscorlib" Version="4.0.0.0"/>
      <StubGeneration>
        <Clear/>
      </StubGeneration>
      <ShimGeneration>
        <Clear/>
        <Add FullName="System.Security.Principal.WindowsIdentity!"/>
      </ShimGeneration>
    </Fakes>
    Friday, January 18, 2013 8:44 PM

All replies

  • I was able to run the following test successfully with Visual Studio 2012 Update 1.

    using (ShimsContext.Create())
    {
        ShimWindowsIdentity.AllInstances.GetName = (identity) => "Blah";
        Assert.AreEqual("Blah", WindowsIdentity.GetCurrent().Name);
    }
    

    Here is my .Fakes file:

    <Fakes xmlns="http://schemas.microsoft.com/fakes/2011/" Diagnostic="true">
      <Assembly Name="mscorlib" Version="4.0.0.0"/>
      <StubGeneration>
        <Clear/>
      </StubGeneration>
      <ShimGeneration>
        <Clear/>
        <Add FullName="System.Security.Principal.WindowsIdentity!"/>
      </ShimGeneration>
    </Fakes>
    Friday, January 18, 2013 8:44 PM
  • I didn't have the bang after the end of the FullName.  I realize it does a case-insensitive search, but why do I need it in this scenario?
    Monday, January 21, 2013 3:30 PM
  • The exclamation mark at the end of FullName is optional. Without it, the Fakes code generator will consider all types that start with the specified string. The exclamation mark tells the code generator to look for an exact match.
    Monday, January 21, 2013 5:22 PM
  • Doesn't work in VS 2012 with update 3. ShimWindowsIdentity.AllInstances class is generated empty. Does anybody else have the same problem?
    Thursday, July 4, 2013 5:06 PM
  • Just verified with VS2012 Update 3; with the .FAKES file posted earlier, ShimWindowsIdentity.AllInstances is generated correctly and the test works. Do you have a small project that can demonstrate the problem?
    Monday, July 8, 2013 4:23 PM