I'm learning about using shims in unit tests. I'm trying the classic example with DateTime, from this link: http://www.wiliam.com.au/wiliam-blog/getting-started-with-microsoft-fakes
I can add Fakes for the System reference in my Unit Test project, but when I then try to use System.Fakes.ShimDateTime, it tells me:
The type or namespace name 'ShimDateTime' does not exist in the namespace 'System.Fakes' (are you missing an assembly reference?)
If I check what's available under System.Fakes, I only see stubs and no shims, so it seems I'm missing something to generate the shims as well?
Not sure if it's relevant, but this is the (default) content from the System.fakes file:
<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
<Assembly Name="System" Version="4.0.0.0"/>
</Fakes>
I'm using Visual Studio 2015. VS2015 14.0.25420.01 Update 3, and my project is running in .NET Framework 4.5.2
In fact my project fails to compile right after adding the fakes for System, so without even trying to use ShimDateTime. The compile error I get is:
The type or namespace name 'EventSourceCreatedEventArgs' does not exist in the namespace 'System.Diagnostics.Tracing' (are you missing an assembly reference?)
And this coming from \UnitTestProject1\obj\Debug\Fakes\m\f.csproj and file f.cs on line: [mqttf::Microsoft.QualityTools.Testing.Fakes.Stubs.StubClass(typeof(global::System.Diagnostics.Tracing.EventSourceCreatedEventArgs))]
Anyone that can put me on the right track to get ShimDateTime available under System.Fakes ?
According to https://connect.microsoft.com/VisualStudio/feedback/details/1049181/fakes-cant-generate-fakes-dlls-for-system-dll this is a bug in the .NET Framework 4.5.
Changing the .NET Framework to 4.5.2 did fix it for me.
I was able to resolve this on Visual Studio 2015 Update 3 for a .NET 4.5.2 project:
error CS0234: The type or namespace name 'ShimDateTime' does not exist in the namespace 'System.Fakes' (are you missing an assembly reference?)
The fix is to actually add the entry to the mscorlib.fakes
XML file (and not as you might presume in System.fakes
)
<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/" Diagnostic="false">
<Assembly Name="mscorlib" Version="4.0.0.0" />
<StubGeneration>
<Clear />
</StubGeneration>
<ShimGeneration>
<Clear />
<Add FullName="System.DateTime!" />
</ShimGeneration>
</Fakes>
I believe this is because, as shown here on MSDN, System.DateTime is actually in the mscorlib
assembly.
I was able to resolve this by manually adding a reference to mscorlib.4.0.0.0.Fakes.dll, which for some reason wasn't included by default.
Credit to: http://www.infoct.net/vs2015-unit-testing-shimdatetime-missing-in-system-fakes-only-stub-functions-743/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With