Reputation: 14927
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!
Upvotes: 0
Views: 1795
Reputation:
It sounds to me like you're trying to test some of your own code that invokes a UrlHelper rather than testing the functionality of the framework's UrlHelper itself. If that is the case, this answer ASP.NET MVC: Unit testing controllers that use UrlHelper has worked great for me.
Upvotes: 2
Reputation: 60674
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.
Upvotes: 1