12

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::Syst‌​em.Diagnostics.Traci‌​ng.EventSourceCreate‌​dEventArgs))]

Anyone that can put me on the right track to get ShimDateTime available under System.Fakes ?

Baz
  • 199
  • 2
  • 5
  • Tried the same approach, start from scratch with a new Unit Test project, added fakes on the System reference, then tried adding your code. I still get an error on ShimDateTime. In fact I immediately have an error when compiling the project after adding the fakes for System: The type or namespace name 'EventSourceCreatedEventArgs' does not exist in the namespace 'System.Diagnostics.Tracing' (are you missing an assembly reference?) – Baz Oct 14 '16 at 10:32
  • The error above is 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))] – Baz Oct 14 '16 at 10:32
  • I have VS2015 14.0.25420.01 Update 3, and my project is running in .NET Framework 4.5.2 – Baz Oct 14 '16 at 10:51
  • I was able to get rid of the compile error by applying the workaround described here: https://connect.microsoft.com/VisualStudio/feedback/details/1049181/fakes-cant-generate-fakes-dlls-for-system-dll But ShimDateTime is still not available. Completely clueless what I'm missing :/ – Baz Oct 14 '16 at 11:35

3 Answers3

6

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.

babelchips
  • 91
  • 1
  • 4
4

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.

Philipp Hofmann
  • 3,388
  • 26
  • 32
3

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/

ramseyjacob
  • 272
  • 3
  • 11