This project is read-only.

Running tests on a TFS build agent

Apr 11, 2013 at 2:25 AM
I've been trying to get some unit tests working on a TFS build agent for a MVC4 application with no luck. I can run the test just fine on my local system, but when it runs on the build machine I get completely different results.
Starting with a default template and the default contact view, this is the test:
        [TestMethod]
        public void Test()
        {
            string exceptionDetails = string.Empty;
            try
            {
                var view = new Contact();
                var message = "Testing";
                view.ViewBag.Message = message;
                HtmlDocument doc = view.RenderAsHtml();

                var headers = doc.DocumentNode.SelectNodes(String.Format("//h2[text()=\"{0}\"]", message));
                Assert.IsTrue(headers.Count() == 1);
            }
            catch (Exception ex)
            {
                exceptionDetails = ex.ToString();
            }

            var assemblies = System.AppDomain.CurrentDomain.GetAssemblies();
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(exceptionDetails);
            foreach (var assembly in assemblies)
            {
                sb.AppendFormat("{0}, \t{1}, \t{2}\r\n", assembly.FullName, assembly.GlobalAssemblyCache, assembly.IsDynamic ? "Dynamic" : assembly.Location);
            }

            sb.Append("done");
            var currentDir = System.Environment.CurrentDirectory;
            Assert.IsTrue(false, sb.ToString());
        }
Comparing the results looks like the biggest difference is the System.Web.Mvc.dll which is 3.0 on the build machine and 4.0 on the local system. Any idea how to convince it to load the right version on the build machine?

The result I get from the build system is:
Assert.IsTrue failed. System.ArgumentNullException: Value cannot be null.
Parameter name: viewContext
   at System.Web.Mvc.AjaxHelper`1..ctor(ViewContext viewContext, IViewDataContainer viewDataContainer, RouteCollection routeCollection)
   at System.Web.Mvc.WebViewPage.InitHelpers()
   at System.Web.Mvc.WebViewPage`1.InitHelpers()
   at System.Web.Mvc.WebViewPage`1.get_ViewData()
   at RazorGenerator.Testing.WebViewPageExtensions.Initialize[TModel](WebViewPage`1 view, HttpContextBase httpContext)
   at RazorGenerator.Testing.WebViewPageExtensions.Render[TModel](WebViewPage`1 view, HttpContextBase httpContext, TModel model)
   at RazorGenerator.Testing.WebViewPageExtensions.RenderAsHtml[TModel](WebViewPage`1 view, HttpContextBase httpContext, TModel model)
   at RazorGenerator.Testing.WebViewPageExtensions.RenderAsHtml[TModel](WebViewPage`1 view, TModel model)
   at MvcApplication2.Tests.Views.Home.UnitTest1.Test() in c:\Builds\87\Retail\Test\src\MvcApplication2.Tests\Views\Home\UnitTest1.cs:line 39
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089,    True,   C:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.dll
Microsoft.VisualStudio.TestPlatform.ObjectModel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a,    False,  C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\Microsoft.VisualStudio.TestPlatform.ObjectModel.dll
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089,  True,   C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll
System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a,    True,   C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll
System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089,  True,   C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll
Microsoft.VisualStudio.TestPlatform.Extensions.VSTestIntegration, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a,   False,  C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\Extensions\Microsoft.VisualStudio.TestPlatform.Extensions.VSTestIntegration.dll
Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a,  True,   C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.QualityTools.UnitTestFramework\10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll
System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089,     True,   C:\Windows\Microsoft.Net\assembly\GAC_64\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll
System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089,     True,   C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll
MvcApplication2.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null,   False,  C:\Builds\87\Retail\Test\bin\MvcApplication2.Tests.dll
MvcApplication2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null,     False,  C:\Builds\87\Retail\Test\bin\MvcApplication2.dll
System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35,  True,   C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.Mvc\v4.0_4.0.0.0__31bf3856ad364e35\System.Web.Mvc.dll
Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a,    True,   C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.CSharp\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.CSharp.dll
System.Dynamic, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a,  True,   C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Dynamic\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Dynamic.dll
System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a,  True,   C:\Windows\Microsoft.Net\assembly\GAC_64\System.Web\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Web.dll
Anonymously Hosted DynamicMethods Assembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null,  False,  Dynamic
System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35,     True,   C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages\v4.0_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.dll
HtmlAgilityPack, Version=1.4.6.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a,     False,  C:\Builds\87\Retail\Test\bin\HtmlAgilityPack.dll
RazorGenerator.Testing, Version=1.5.3.0, Culture=neutral, PublicKeyToken=null,  False,  C:\Builds\87\Retail\Test\bin\RazorGenerator.Testing.dll
System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35,  True,   C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.Mvc\v4.0_3.0.0.0__31bf3856ad364e35\System.Web.Mvc.dll
System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35,     True,   C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages\v4.0_1.0.0.0__31bf3856ad364e35\System.Web.WebPages.dll
Moq, Version=4.0.10827.0, Culture=neutral, PublicKeyToken=69f491c39445e920,     False,  C:\Builds\87\Retail\Test\bin\Moq.dll
ReflectionMagic, Version=1.0.1.0, Culture=neutral, PublicKeyToken=null,     False,  C:\Builds\87\Retail\Test\bin\ReflectionMagic.dll
DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=a621a9e7e5c32e69,    False,  Dynamic
Running it locally I get: (Summary)
  mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089,  True,   C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll
  System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089,    True,   C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll
  MvcApplication2.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null,     False,  C:\Work\TestSite\MvcApplication2\MvcApplication2.Tests\bin\Debug\MvcApplication2.Tests.dll
  System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a,    True,   C:\Windows\Microsoft.Net\assembly\GAC_32\System.Web\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Web.dll
  System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089,   True,   C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll
  MvcApplication2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null,   False,  C:\Work\TestSite\MvcApplication2\MvcApplication2.Tests\bin\Debug\MvcApplication2.dll
  System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35,    True,   C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.Mvc\v4.0_4.0.0.0__31bf3856ad364e35\System.Web.Mvc.dll
  System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35,   True,   C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages\v4.0_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.dll
  RazorGenerator.Testing, Version=1.5.3.0, Culture=neutral, PublicKeyToken=null,    False,  C:\Work\TestSite\MvcApplication2\MvcApplication2.Tests\bin\Debug\RazorGenerator.Testing.dll
  HtmlAgilityPack, Version=1.4.6.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a,   False,  C:\Work\TestSite\MvcApplication2\MvcApplication2.Tests\bin\Debug\HtmlAgilityPack.dll
  Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a,  True,   C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.CSharp\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.CSharp.dll
  System.Dynamic, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a,    True,   C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Dynamic\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Dynamic.dll
  Moq, Version=4.0.10827.0, Culture=neutral, PublicKeyToken=69f491c39445e920,   False,  C:\Work\TestSite\MvcApplication2\MvcApplication2.Tests\bin\Debug\Moq.dll
  ReflectionMagic, Version=1.0.1.0, Culture=neutral, PublicKeyToken=null,   False,  C:\Work\TestSite\MvcApplication2\MvcApplication2.Tests\bin\Debug\ReflectionMagic.dll
Apr 11, 2013 at 5:31 AM
And they both have the exact same version of System.Web.WebPages? Sometimes the assembly version may be the same but the file version may not.

The RazorGenerator test assembly has:
    view.ViewContext = new ViewContext(controllerContext, new Mock<IView>().Object, view.ViewData, new TempDataDictionary(), new StringWriter());
So we're trying the set the ViewContext, but in your broken case, it looks like the act of asking for view.ViewData blows up because it doesn't like that view.ViewContext is null (which it would be before the assignment).
Apr 11, 2013 at 6:40 PM
Edited Apr 11, 2013 at 9:06 PM
Thanks for the reply,

Interesting, System.Web.WebPages is different on the two:
Local: System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35,
Build Server: System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35,
The project file has V2 as a reference though.

What's puzzling is that the build server has both 1 and 2 installed, where local computer has only 1 installed. Uninstalling 1 from the build server results in:
Test method MvcApplication2.Tests.Views.Home.UnitTest1.Test threw exception: 
System.IO.FileLoadException: Could not load file or assembly 'System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition 
Installing ASP.NET and Web Tools 2012.2 on the build server (not ideal) helped with the versions but not the exception.