2012-11-29 4 views
0

MVC 3 경로에 대한 테스트를 작성하려고하지만 아직 일치하지 않습니다. 왜 이것이 시험에서 작동하지 않습니까? http://haacked.com/archive/2007/12/17/testing-routes-in-asp.net-mvc.aspx에서 MVC 코드를보고 VirtualPathProvider와 관련이 있습니다. 당신은 TestRoutes 방법에서이 줄을 HttpMethod을 설정, 그래서 추가해야 MOQ아직 다른 ASP.NET MVC 경로를 찾을 수 없습니다.

[TestMethod] 
    public void TestRoutes() 
    { 
     string url = "~/en-us/Client/1/DownloadTemplate"; 

     var httpContextMock = new Mock<HttpContextBase>(); 
     httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath) 
      .Returns(url); 

     RouteCollection routes = new RouteCollection(); 
     MvcApplication.RegisterRoutes(routes); 

     var routeData = routes.GetRouteData(httpContextMock.Object); 
     // routeData is null! 

     Assert.AreEqual("import", routeData.Values["controller"].ToString()); 
     Assert.AreEqual("DownloadTemplate", routeData.Values["action"].ToString()); 
    } 

답변

0

를 사용

Global.asax에

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

    routes.MapRoute(
     "import_DownloadTemplate", 
     "{culture}/Client/{clientId}/DownloadTemplate", 
     new { controller = "Import", action = "DownloadTemplate" }, 
     new { httpMethod = new HttpMethodConstraint("GET") }); 
} 

ImportController

[HttpGet] 
[Cache(Order = 1)] 
[OutputCache(Order = 2, Duration = 60, VaryByParam = "*")] 
public ActionResult DownloadTemplate(string culture, long clientId) 
{ 
    byte[] result = this.repository.GetTemplateByClientId(clientId, culture); 

    return new FileContentResult(result, "application/vnd.ms-excel"); 
} 

테스트 :

string httpMethod = "GET"; 
httpContextMock.Setup(c => c.Request.HttpMethod).Returns(httpMethod); 
+0

문제를 해결 했습니까? –

관련 문제