Now UrlHelper has been made friendly for unit testing I wanted to replace old test code with a mocked version. However I can't seem to get T4MVC's Action extension method to actually call the mocked method, it keeps hitting UrlHelper's method according to the stack trace.
I've tried setting the Url property on the controller to be a Moq object and also subclassed UrlHelper. I've verified both are getting called by debugging the method.
Just using strict mocking to try and generate the right exception:
_url = new Mock<UrlHelper>(MockBehavior.Strict);
_controller.Url = _url.Object;
_controller.Url.Action(MVC.Home.Index()); //Should throw UrlHelper.RouteUrl not mocked exception
Instead I get:
System.ArgumentNullException : Value cannot be null.
Parameter name: routeCollection
at System.Web.Mvc.UrlHelper.GenerateUrl(String routeName, String actionName, String controllerName, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, Boolean includeImplicitMvcValues)
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.RouteUrl(String routeName, RouteValueDictionary routeValues, String protocol, String hostName)
at System.Web.Mvc.T4Extensions.Action(UrlHelper urlHelper, ActionResult result, String protocol, String hostName)
The code is going straight from T4MVCExtensions to UrlHelper.
If I copy the code from the T4MVC extension method into a new extension method in the web project the mocked UrlHelper does get called and everything works as expected.
public static class UrlExtensions
{
public static string Action(this UrlHelper urlHelper, ActionResult result)
{
return urlHelper.RouteUrl(null, result.GetRouteValueDictionary(), result.GetT4MVCResult().Protocol, null);
}
}
Have I missed something fundamental about extension methods? I can't work out what is going on which might cause the extension method to be using the parent class implementation. For reference I'm using Resharper 8.1 to run the tests, Moq 4.2 and NUnit 2.6.4 and T4MVCExtensions 3.11.0.