Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Faking DateTime Using Microsoft Fakes Seems to Be Broken

DZone's Guide to

Faking DateTime Using Microsoft Fakes Seems to Be Broken

Microsoft provides developers with Mocking for their unit tests with Microsoft Fakes, but it has some glaring issues around DateTime that the author has a nice workaround.

· Web Dev Zone
Free Resource

Tips, tricks and tools for creating your own data-driven app, brought to you in partnership with Qlik.

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

image

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 nowhere 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:

image

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.

The Solution?

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:

image

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="4.0.0.0"/>
  <StubGeneration>
    <Remove FullName="System.Diagnostics.Tracing"/>
    <Remove FullName="System.Text.Encoding"/>
    <Remove FullName="System.Security.Cryptography" />
  </StubGeneration>
</Fakes>

Next, I’ve deleted System.4.0.0.0.Fakes and system.fakes and right-clicked System->Add Fakes Assembly and got the following:

image

And now I was able to write my test using my DateTime of choosing.

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.

Explore data-driven apps with less coding and query writing, brought to you in partnership with Qlik.

Topics:
c# ,mock objects ,unit tests ,ms fakes ,web dev

Published at DZone with permission of Dror Helper, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}