1
Vote

GetRouteDataForUrl return RouteData in

description

In route config I have:
public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "home", action = "index", id = UrlParameter.Optional },
        namespaces: new string[] { "CreditoImobiliarioBB.Web.Controllers" }
    );
}
And my test
[TestMethod]
public void Novo_deve_ser_mapeado()
{
    // Arrange
    var routes = new RouteCollection();
    RouteConfig.RegisterRoutes(RouteTable.Routes);

    // Act
    var routeData = routes.GetRouteDataForUrl("usuarios/novo"); // Will return null

    // Asserts
    routeData.Should()
        .HaveController("usuarios")
        .HaveAction("novo");
}
What am I missing?

comments

KevinKuszyk wrote Jul 17, 2013 at 4:34 PM

When you call RegisterRoutes() you need to use the route collection you just created.

Change your arrange to this and it will work:
// Arrange
var routes = new RouteCollection();
RouteConfig.RegisterRoutes(routes);