views:

93

answers:

1

I'm having a tough go trying to figure out what I need to mock in my tests to show that UrlHelper.RouteUrl() is returning the right URL. It works, but I'd like to have the right test coverage. The meat of the controller method looks like this:

var urlHelper = new UrlHelper(ControllerContext.RequestContext);
return Json(new BasicJsonMessage { Result = true, 
   Redirect = urlHelper.RouteUrl(new { controller = "TheController", 
   action = "TheAction", 
   id = somerecordnumber }) });

Testing the result object is easy enough, like this:

var controller = new MyController();
var result = controller.DoTheNewHotness());
Assert.IsInstanceOf<JsonResult>(result);
var data = (BasicJsonMessage)result.Data;
Assert.IsTrue(data.Result);

result.Redirect is always null because the controller obviously doesn't know anything about the routing. What do I have to do to the controller to let it know? As I said, I know it works when I exercise the production code, but I'd like some testing assurance. Thanks for your help!

+1  A: 

Isn't UrlHelper.RouteUrl() a framework method? That's the .NET team's responsibility to test, not yours!

What you might want to do is to unit test your route configuration. This question is really old (MVC Beta 1), but the answer might still be valid. And Phil Haack's Routing Debugger is always there for you. If that's not enough, there are tons of other options on Google.

Tomas Lycken
In this case, I'm not testing the framework code as much as I want to test that the framework code is getting the right data from me. Does that make sense?
Jeff Putz
@Jeff, It does, in a way, but it would seem you're doing one step too much. Have you checked out the fluent route tester? http://flux88.com/blog/fluent-route-testing-in-asp-net-mvc/
Tomas Lycken