views:

33

answers:

1

I've got the default route set-up:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
            "Shortie", // Route name
            "{controller}/{id}", // URL with parameters
            new { controller = "Ettan", action = "Index", id = "id" } // Parameter defaults
            );

    routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Ettan", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
}

I'm running the following test in nunit/resharper, using Moq to setup the HttpContext:

[TestFixture]
public class RoutesTests
{
    [Test]
    public void RoutingTest()
    {
        var routes = new RouteCollection();
        MvcApplication.RegisterRoutes(routes);

        AssertRoute(routes, "~/", "Ettan", "Index");
        AssertRoute(routes, "~/Artikel/123", "Artikel", "Index");

    }

    public static void AssertRoute(RouteCollection routes, string url, string controller, string action)
    {
        var httpContext = new Mock<HttpContextBase>();
        httpContext.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/");

        var routeData = routes.GetRouteData(httpContext.Object);


        Assert.IsNotNull(routeData);
        Assert.AreEqual(controller, routeData.Values["Controller"]);
        Assert.AreEqual(action, routeData.Values["Action"]);
    }
}

I'm expecting both AssertRoutes to success, but the second one fails (it routes to controller = "Ettan", not controller = "Artikel").

However, if I run the mvc application and browse to localhost/Artikel/123, I will be routed to the Artikel controller. Why is this? Something wrong with my tests?

+3  A: 

In AssertRoute, you are setting up httpContext.Request.AppRelativeCurrentExecutionFilePath to always return "~/" instead of the url you want to test.

adrift
Yep, it was too obvious. Thanks.
Jonas Lincoln