It seems as if causing DateTime.Now to return another value has become the demo to show when demoing an unconstrained Mocking framework. It’s easy to show and needed in many unit tests – unless you want your tests to be affected by time – trust me and you don’t.
That’s why I was amazed to find out that the demo from MSDN which showed how to use Shims has somehow stopped working…
What I initially did
In order to start using Microsoft Fakes you need to use Visual Studio Enterprise edition – I’ve used VS2015 update 5 in this case.
The next step is to right click on the assembly you need to fake and choose Add Fake Assembly
this will generate the same assembly followed .fakes so if you’re faking MyAssembly a new MyAssembly.Fakes will be created this is not the case with System as I was quickly reminded.
Now according to my knowledge and the demos at hand all I needed to do now is find ShimDateTime and use some magic to change how it behaved – but it was no where to be found – in fact I could not see anything to do with DateTime which I know for certain existed inside System.
It took me a while to get it but looking at the project’s references showed the problem:
You see – usually when faking System two fake assemblies are created – one for System and one for mscorlib where the good stuff is.
You can even see that under Fakes folder there are two configuration files one for each – but no mscorlib to be found.
I did what any developer would have done: I’ve decided to repeat the same process again expecting different results. Actually first I Googled a bit and found very little information.
And so I need to delete three files – the fakes assembly and two configuration files (fake configuration?) and try again – and I got the exact same mess.
After trying two more time – just to make sure I’ve decided to check the output window and saw the following:
Armed with this new knowledge I’ve decided to dig into some documentation and found that some new breaking changes caused issues when creating fakes and quick workaround would be to remove a few classes from the “To fake list” and so I’ve opened mscorlib.fakes configuration file and written (read: pasted) the following code:
<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/"> <Assembly Name="mscorlib" Version="184.108.40.206"/> <StubGeneration> <Remove FullName="System.Diagnostics.Tracing"/> <Remove FullName="System.Text.Encoding"/> <Remove FullName="System.Security.Cryptography" /> </StubGeneration> </Fakes>
Frustrating? you bet! but at least now I know a quick workaround and able to test time based code – if all I have is Microsoft Fakes.