chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hello,

How do I unit test ASP.NET MVC routes using typemock isolator?
asked by sudhir (3.5k points)

4 Answers

0 votes
Hello,

I apologize for the late reply.

I would recommend reading this article by Phil Haack, regarding testing routes in ASP.NET MVC.

The examples in the article are using Moq to fake HttpContextBase, but you could do the same with Isolator:

var fakeHttpContext = Isolate.Fake.Instance<HttpContextBase>();
Isolate.WhenCalled(() => fakeHttpContext.AppRelativeCurrentExecutionFilePath)
        .WillReturn("~/product/list");


Hope that helps.
answered by igal (5.7k points)
0 votes
Hello igal,
I am not finding the parameter "AppRelativeCurrentExecutionFilePath".
Could u help me here. I have pasted the code taht I wan to test.
----Code-----
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Controller1", action = "Action1", id = UrlParameter.Optional } // Parameter defaults

);

}
-----------Code--------------

What could be the test for this. I have tryed many times, but still not working.
Your help will be appriciated.
answered by sudhir (3.5k points)
0 votes
I accidentally left out an important piece when copy pasting code, sorry for that! :oops:

Here's a complete test for the route:

[TestMethod]
public void RouteTest()
{
    RouteCollection routes = new RouteCollection();
    routes.MapRoute(
        "Default", // Route name 
        "{controller}/{action}/{id}",
        new { controller = "Controller1", action = "Action1", id = UrlParameter.Optional });

    var fakeHttpContext = Isolate.Fake.Instance<HttpContextBase>();
    Isolate.WhenCalled(() => fakeHttpContext.Request.AppRelativeCurrentExecutionFilePath)
        .WillReturn("~/home/index");

    RouteData routeData = routes.GetRouteData(fakeHttpContext);

    Assert.AreEqual("home", routeData.Values["Controller"]);
    Assert.AreEqual("index", routeData.Values["action"]);
}


Also, if you're looking for a way to just visualize and debug your routes, you could use Phil Haack's great tool for that: http://haacked.com/archive/2008/03/13/u ... ugger.aspx

Good luck!
answered by igal (5.7k points)
0 votes
Thanks.
answered by sudhir (3.5k points)
...