How do I test UrlHelper.RouteUrl()?

Posted by Jeff Putz on Stack Overflow See other posts from Stack Overflow or by Jeff Putz
Published on 2010-05-24T06:01:43Z Indexed on 2010/05/24 6:21 UTC
Read the original article Hit count: 1553

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!

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about asp.net-mvc