Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

When I run this Unit Test I encounter error in routes.MapMvcAttributeRoutes(); it says "This method cannot be called during the application's pre-start initialization phase."

Any suggestion on how can my unit test will works? or can I mock the MapMvcAttributeRoutes?

/// Route Config
public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapMvcAttributeRoutes();

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

    [TestMethod]
    public void Should_Map_Admin_Action_Route()
    {
        /// Arrange
        var routes = new RouteCollection();
        RouteConfig.RegisterRoutes(routes);

        var httpContextMock = new Mock<HttpContextBase>();
        httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath)
                       .Returns("~/admin");

        /// Act
        RouteData routeData = routes.GetRouteData(httpContextMock.Object);

        /// Assert
        Assert.IsNotNull(routeData);
        Assert.AreEqual("admin", routeData.Values["Controller"]);
        Assert.AreEqual("Index", routeData.Values["action"]);
    }
share|improve this question
    
I found answer to my question ^_^. github.com/AnthonySteele/MvcRouteTester –  abstractOverride May 19 at 10:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.