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"]);
}