Running unit tests that use Microsoft Fakes on Team Build

As you may know I worked on TFS Branch Tool which is one of the ALM Rangers solutions. As usual we are dogfooding latest technology and at some point our builds started failing. We tried to investigate cause and found that product team deployed latest VS 2013 bits into golden image of the agent. We identified that some of the tests that use Microsoft Fakes are failing on hosted build. Don’t be surprised that everything was fine on developer’s computer. So, business as usual.

As it often happens in developer’s life finding of the root cause usually takes 90% of time that needed for solving an issue. Symptoms were very strange – failing unit tests behaved same way like Fakes aren’t running at all. In our case we tested the code that uses Team Foundation Client OM and we had shims to avoid connection to real Team Foundation Server. So, tests are failing because code under test is trying to set up connection to real server.

To narrow down the problem I made few tests proving that Fakes are working on hosted team build if I shimming System.DateTime for example. Then I’ve created fresh new test project and put tests of Team Foundation Client here. Obviously, it working fine. Then it took just few minutes to find a difference between initial project and new one.

Here what I found. Historically Branch Tool was designed using Visual Studio 2012 and Team Foundation Server 2012 object model. Thus, we had references to v. 11 of Microsoft.TeamFoundation.* assemblies. For some reason all references had “Specific Version” option set to “False”. That would allow IDE to use newer version of that assembly if found on the computer. In the project files this looks like:

<ReferenceInclude=Microsoft.TeamFoundation, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL>
  <HintPath>..\..\..\..\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.dll</HintPath>

Reference resolution works pretty consistent between Visual Studio and Team Build but not for the Fakes. It appears that Fakes engine resolves references different way when executing via Team Build. It ignoring value of the “Specific Version” option and always considering it as the true.

We getting into the trouble as soon as we have newer version of Microsoft.TeamFoundation assembly on the computer. Code under test is compiled with reference to v. 12 of the assembly. Fakes framework is ignoring attempt to load of Team Foundation object model types as it expects v. 11. As result, all shims aren’t working at all.

Resolution is very simple – set “Specific Version” option to “True” and use v. 11 assembly explicitly or change reverence to v. 12 assembly if you need fresh Team Foundation object model.