Generating Fakes assembly for System.dll for unit test projects

With the release of .NET Framework 4.6.1 and 4.6.2, many customers reported that they are facing build errors while generating/using Fakes assembly for System.dll in Visual Studio 2015 Update 3 and Visual Studio 2017 RC. They were getting errors similar to this:

error C50234: The type or namespace name 'EventSourceCreatedEventArgs' does not exist in namespace "System.Diagnostics.Tracing".

For example, when we attempt to generate fakes for System.dll in a unit test project on Visual Studio 2015 U3 that targets framework version 4.6.1 and latest framework installed on machine is version 4.6.2, build fails with errors as shown in snapshot below:

pic1

It is recommended to target the unit test projects to latest version of .NET framework installed on the machine if Fakes for System.dll is used. In the example mentioned above, if the target framework version of test project is changed to 4.6.2, fakes assemblies for System.dll and mscorlib.dll get generated successfully.

This restriction is because of the fact that .NET CLR always loads the latest version of mscorlib.dll irrespective of the target framework version selected in the project. Since generating fakes for system.dll generates fakes assembly for mscorlib.dll as well, this results in build errors for types in which breaking changes are introduced in later versions of mscorlib.dll and System.dll

We have fixed this in the latest Visual Studio 2017 RC. For Visual Studio 2015 U3 customers, please try out the steps mentioned below and do let us know how it goes.

In order to fix this issue, we need to remove stub and shim generation for types in which breaking changes are introduced between the target framework version of test project and latest framework version installed on machine.

Steps to remove shim and stub generation for types with breaking changes :

1. Right click on System.dll in Solution Explorer and click on "Add Fakes Assembly".

pic2

2. This will result in build failure because of breaking changes introduced between target framework version (v4.6) and lastest target framework installed on machine(v4.6.2).

pic3

3. The above step should have added a new folder "Fakes" in unit test project, if not, create it manually.

pic4

4. Modify the content of these files by copying the content from mscorlib.fakes and System.fakes.

5. Rebuild the project.

pic5

6. Please note that the mscorlib.4.0.0.0.fakes assembly is not referenced. Manually add reference to this assembly under FakesAssemblies folder present at project root folder.

pic6