Note that there are some explanatory texts on larger screens.

plurals
  1. POUrlHelper's RouteUrl returning Empty String in Tests
    primarykey
    data
    text
    <p>I am having an issue where UrlHelper's RouteUrl method only returns an empty string when run in my tests, though function properly when executing in the real HttpContext. It is, however, finding the route - as I do properly get an exception if I try to resolve a route name which has not been defined.</p> <p>I have mocked the HttpContext and friends using the <a href="http://www.hanselman.com/blog/ASPNETMVCSessionAtMix08TDDAndMvcMockHelpers.aspx" rel="nofollow noreferrer">code provided by Scott Hanselman/Kzu</a> and added the <a href="https://stackoverflow.com/questions/674458/asp-net-mvc-unit-testing-controllers-that-use-urlhelper">code needed to bootstrap the Application's Routes</a> into the mocked instance </p> <p>To reduce the number of variables in my situation, I've written a simple test:</p> <pre class="lang-cs prettyprint-override"><code>[Test] public void UrlHelperReturnsCorrectUrl() { var controller = new MyController(); controller.SetFakeControllerContext().LoadUrlHelper(); Assert.AreEqual("My/Route/Path", controller.Url.RouteUrl("MyRoute")); } </code></pre> <p>Interestingly enough, accessing the RouteCollection directly and using VirtualPath does work:</p> <pre class="lang-cs prettyprint-override"><code>[Test] public void GetVirtualPathReturnsCorrectUrl() { var controller = new AccountController(); controller.SetFakeControllerContext().LoadUrlHelper(); Assert.AreEqual("My/Route/Path", Controller.Url.RouteCollection["MyRoute"] .GetVirtualPath( controller.Url.RequestContext, new RouteValueDictionary()) .VirtualPath); } </code></pre> <p>For reference, Here is my implementation of the LoadUrlHelper extension method:</p> <pre class="lang-cs prettyprint-override"><code>public static Controller LoadUrlHelper(this Controller controller) { var routes = new RouteCollection(); MvcApplication.RegisterRoutes(routes); controller.Url = new UrlHelper( controller.ControllerContext.RequestContext, routes); return controller; } </code></pre> <p>And here is my route as defined in my application's Global.asax:</p> <pre class="lang-cs prettyprint-override"><code>routes.MapRoute( "MyRoute", "My/Route/Path", new {controller = "Home", action = "Index"}); </code></pre> <p>Has anyone run into this? Am I missing something?</p> <p>EDIT:</p> <p>I've followed the MVC code down to the point that it hands the route processing off to System.Routing and found something very interesting. The code that MVC eventually runs to lookup the desired URL (condensed, of course) returns an empty string:</p> <pre class="lang-cs prettyprint-override"><code>Controller.Url.RouteCollection.GetVirtualPath( Controller.Url.RequestContext, "MyRoute", new RouteValueDictionary()).VirtualPath; </code></pre> <p>whereas an extremely similar variant returns the expected string:</p> <pre class="lang-cs prettyprint-override"><code>Controller.Url.RouteCollection["MyRoute"].GetVirtualPath( Controller.Url.RequestContext, new RouteValueDictionary()).VirtualPath; </code></pre> <p>I can't seem to go any further in the underlying code to see what is actually happening differently here, but thought it might help someone understand what setup I am missing. (I'm not going to yell bug yet, as the fact stands that the UrlHelpers do work when in a real HttpContext)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload