This project is read-only.

Url.Action Throws Error (Again?)

Sep 4, 2013 at 11:50 PM
Hi all,

Back last year, I was working with RazorGenerator and reported that views with a Url.Action call were throwing a null reference error. If I recall correctly, there was a fix, and my unit tests were going swimmingly - until the latest update. What changed?

I'm using the same mocks as previously:
private Mock<HttpContextBase> mockContext;
private Mock<HttpSessionStateBase> mockSession;
private Mock<HttpRequestBase> mockRequest;
private Mock<HttpResponseBase> mockResponse;
mockContext = new Mock<HttpContextBase>();
mockSession = new Mock<HttpSessionStateBase>();
mockRequest = new Mock<HttpRequestBase>();
mockResponse = new Mock<HttpResponseBase>();
mockContext.Setup(m => m.Request).Returns(mockRequest.Object);
mockContext.Setup(m => m.Response).Returns(mockResponse.Object);
mockContext.Setup(m => m.Session).Returns(mockSession.Object);      
Is there something additional now that needs to be set up?

Thanks

Jim Stanley
Blackboard Connect

Here's the stack trace:

at System.Web.Mvc.UrlHelper.GenerateUrl(String routeName, String actionName, String controllerName, String protocol, String hostName, String fragment, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, Boolean includeImplicitMvcValues)
at System.Web.Mvc.UrlHelper.Action(String actionName, String controllerName, Object routeValues, String protocol)
at ShimmerIVR.Views.Replay.ReplaySelectInstitution.Execute() in c:\TFS12\BCI\Branches\Release5.7\Web\Site\BCI.Web.Site.ShimmerIVR\ShimmerIVR\Views\Replay\ReplaySelectInstitution.cshtml:line 44
at RazorGenerator.Testing.WebViewPageExtensions.Render[TModel](WebViewPage`1 view, HttpContextBase httpContext, TModel model)
at IVRUnitTests.ReplayTests.BizarreInstitutionCharactersTest() in c:\TFS12\BCI\Branches\Release5.7\Web\Site\BCI.Web.Site.ShimmerIVR\IVRUnitTests\ReplayTests.cs:line 182
Sep 9, 2013 at 8:04 PM
Are you sure this was with the same version of MVC? It's possible that they changed some subtle things that may have broken this.
Feb 8, 2014 at 12:05 AM
Hi David,

I just finished upgrading the project to MVC 5 (Razor 3) under VS 2013. RazorGenerator is version 2.2.1, also with SimpleInjector version 2.4.1. Still getting the same issues with trying to render a Url.Action. Full error detailed below:

System.NullReferenceException was unhandled by user code
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=System.Web.Mvc
StackTrace:
   at System.Web.Mvc.UrlHelper.GenerateUrl(String routeName, String actionName, String controllerName, String protocol, String hostName, String fragment, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, Boolean includeImplicitMvcValues)
   at System.Web.Mvc.UrlHelper.Action(String actionName, String controllerName, Object routeValues, String protocol)
   at ShimmerIVR.Views.Replay.ReplaySelectInstitution.Execute() in c:\TFS2013\Branches\Release5.9\Web\Site\BCI.Web.Site.ShimmerIVR\ShimmerIVR\Views\Replay\ReplaySelectInstitution.cshtml:line 44
   at RazorGenerator.Testing.WebViewPageExtensions.Render[TModel](WebViewPage`1 view, HttpContextBase httpContext, TModel model)
   at IVRUnitTests.ReplayTests.BizarreInstitutionCharactersTest() in c:\TFS2013\Branches\Release5.9\Web\Site\BCI.Web.Site.ShimmerIVR\IVRUnitTests\ReplayTests.cs:line 182
InnerException:

Do I have to do some sort of other mocking to get the context? If I recall from two years ago, that was well-nigh impossible.

Would it help if I sent along the generated file from the view?

(As our division is moving to a "unit tests must pass before checkin" policy, let's say I have more than a passing interest in getting this to work!)

Thanks

Jim Stanley
Blackboard Connect
Feb 10, 2014 at 4:32 AM
To be honest, the unit testing support was always a bit experimental. I got it working way back (maybe MVC2 timeframe?), and it took some hacks to mock up everything. e.g. look at this file, where I had to do some private reflection. It's possible that all this needs to be tweaked to work with MVC5.

I have very little time myself to work on this, but I would happily accept contributions if you get it working! :)
Feb 14, 2014 at 12:19 AM
Hi David,

Thanks for the pointer to the file. Is there any way to incorporate the source code into a test solution so I can try fiddling with it? Looking at the MVC docs for WebPageHelper, it's probably something with the routing that's throwing the error, but it would of course be simpler to find by incorporating source.

Hope this isn't Stupid Question #37A. At least, if I do find a solution, I'll happily contribute it back!

Thanks

Jim Stanley
Blackboard Connect
Feb 14, 2014 at 12:52 AM
You mean the source code for the RazorGenerator test helpers? If so, I'd think you could just copy them into your project and fiddle with it there. Would love to make this work again!

thanks,
David