MVC4 breaks RazorGenerator.Testing


It's as simple as that.

I upgraded the MVC3 project I had been working on to MVC4, and after having resolved all the references etc found my unit tests were failing - deep in the bowels of MVC, the AjaxHelper constructor was throwing an ArgumentNullException (note: NOT a NullReferenceException) because the view context was null.

To prove it wasn't me going mad I went and created 2 vanilla empty MVC sites - one in MVC3 and one in MVC4. I then added a class library to each one and nugetted NUnit.

Then I added the latest nuget packages for RazorGenerator.Mvc to the respective web apps, and RazorGenerator.Testing to the unit test projects.

I added a HelloWorldController to each web app, and right clicked on the Index method to create the View. Just to make sure the view was correct I added some Hello World text to the view so we could be sure that it was this view being rendered when run in the browser.

I then created a unit test in each unit test folder, which simply instantiates the view Index (which works), and then calls the RenderAsHtml<TModel>(model) extension method defined in the testing package (which breaks in MVC4, but not in MVC3).

Sure enough, MVC3 worked fine, but MVC4 gave me the error as described above.

Further investigation led me to find that it seems to be the way that the ReflectionMagic package interacts with System.Web.WebPages 2.0, when the various mock objects are being created, prior to rendering.

The ironic thing is that copying the code out of the site (http://razorgenerator.codeplex.com/SourceControl/changeset/view/da5af667e8a8#RazorGenerator.Testing/WebViewPageExtensions.cs) and placing it in the same assembly as the test code actually seemed to fix this! However, a very important caveat is that the code MUST be in the same assembly - if it's in, say, a testing infrastructure common assembly, this will not only cause MVC to throw an exception - it will crash the NUnit agent process with an ExecutionEngineException...

Obviously this is quite a serious issue, as it would be much better to use the package as-is, rather than have to go to codeplex and copy the code manually into my test project.


davidebbo wrote Jan 26, 2013 at 12:53 AM

Strange. If you manage to figure out the root of the problem and have a fix which works with both MVC3 and MVC4, please send a pull request and we'll get that in.

We have limited time to work on RazorGenerator, so we can use any help we can get :)

pranavkm wrote Jan 28, 2013 at 3:41 AM

Does your test project have the right binding redirects to Mvc? If you've added the Mvc reference via Microsoft.AspNet.Mvc package, NuGet would add it for you, if you added the reference from GAC have to manually add it by calling Add-BindingRedirect (or copying it from your web.config).

I tested this using XUnit and VS 2012's test runner and adding the binding redirects fixed it for me.