Question

I am in the process of converting our unit tests from Moles to the new VS 2012 Fakes. Several of our unit tests "fake" RNGCryptoServiceProvider. We were able to "mole" this out but it seems there is no Shim created for it in the Fakes. In other words, I would expect to find a ShimRNGCryptoServiceProvider.

An even more interesting aspect is that I found an eBook online called "Better Unit Testing with Microsoft Fakes". In there they show an example of faking the Random function. Here is the example.

System.Fakes.ShimRandom.Constructor = (real) => { };        
System.Fakes.ShimRandom.AllInstances.NextDouble = this.NextDouble; 
System.Fakes.ShimRandom.AllInstances.NextInt32Int32 = this.NextInt32Int32; 
private int NextInt32Int32(Random random, int i, int arg3) 
{ 
    return (i + arg3) / 2; 
}

I don't even see the System.Fakes.ShimRandom in my project. The only two Shims I see for System.Fakes is ShimDateTime and ShimGuid.

  • I created the Fakes assembly for System (which gave me mscorlib.4.0.0.0.Fakes and System.4.0.0.0.Fakes)
  • I have not changed the configuration files (mscorlib.fakes and System.fakes)
  • I have my Fakes project set to build in "Any CPU" platform.
  • We are using .Net Framework 4.5 and VS 2012 Ultimate.

I do see a bunch of Stubs, including System.Fakes.StubRandom and System.Fakes.StubRandomNumberGenerator, but Stubs won't work for me because I have no way of injecting them into the code under test.

  1. Any idea why I am not getting a ShimRandom when it seems I should be since there is an example of it in an eBook?
  2. How do I "fake" RNGCryptoServiceProvider?
Était-ce utile?

La solution

These types are currently not supported by the MS Fakes framework.

enter image description here

Another way to find the unsupported types are, in your .fakes file add Diagnostic = true For example.

<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/" Diagnostic ="true">
   <Assembly Name="mscorlib" Version="4.0.0.0"/>

UPDATED

Note that you just can't Fake everything. That also means you just can't Shim everything either. MS has decided not to Shim some system classes because of some Design Considerations. There is no definite list that MS has provided because the types can be Fake'd based on the combination of user's .NET versions and targeted .NET frameworks. For example, in .NET 4, some members in System.Security, types in System.Threading Fakes won't get generated so as Shims.

You can try overriding this behaviour, for example adding the following xml to the .Fakes file

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

The above would produce the same warnings which I have provided in my answer above. This means they are not supported.

But as I said before, it is a combination of .NET version and the target framework. If you can change targeted .NET frameowork, for instance .NET 2, still using the mscorlib version 4.0.0.0, you see the Shim get generated for RNGCryptoServiceProvider.

 <Fakes xmlns="http://schemas.microsoft.com/fakes/2011/" Diagnostic="true" TargetFrameworkVersion="2.0.0.0">
   <Assembly Name="mscorlib" Version="4.0.0.0"/>
     <ShimGeneration>
      <Clear/>
        <Add FullName="System.Security.Cryptography"/>
     </ShimGeneration>
 </Fakes>

The same is applied to RandomNumberGenerator.

enter image description here

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top