0

MVC 3 ルートのテストを作成しようとしていますが、まだ一致しません。これがテストで機能しないのはなぜですか?私はhttp://haacked.com/archive/2007/12/17/testing-routes-in-asp.net-mvc.aspxを離れます.MVCコードを見ると、VirtualPathProviderと関係があるかもしれません?

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

MOQを使用したテスト

    [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());
    }
4

1 に答える 1

0

HttpMethod を設定する必要があるため、次の行を TestRoutes メソッドに追加します。

string httpMethod = "GET";
httpContextMock.Setup(c => c.Request.HttpMethod).Returns(httpMethod);
于 2012-11-29T21:55:12.453 に答える