asp.net-mvc - 测试 MVC Controller 方法的 URL

标签 asp.net-mvc testing routes

我看过一些关于测试 Microsoft 路由的非常有用的帖子。特别是 www.strathweb.com/2012/08/testing-routes-in-asp-net-web-api/似乎只处理 WebApi。虽然相似,但它们并不相同。如果我有一个 MVC 应用程序,我如何查看将为给定 URL 调用的方法。它似乎归结为创建一个可以传递给 HttpControllerContext 的构造函数并在测试中获取对“当前”配置(如 HttpConfiguration)的引用的“请求”。想法?

谢谢。

最佳答案

测试传入 URL

如果您需要测试路由,您需要从 MVC 框架中模拟三个类:HttpRequestBase、HttpContextBase 和 HttpResponseBase(仅用于传出 URL)

private HttpContextBase CreateHttpContext(string targetUrl = null, string httpMethod = "GET")
    {
        // create mock request
        Mock<HttpRequestBase> mockRequest = new Mock<HttpRequestBase>();
        // url you want to test through the property 
        mockRequest.Setup(m => m.AppRelativeCurrentExecutionFilePath).Returns(targetUrl);
        mockRequest.Setup(m => m.HttpMethod).Returns(httpMethod);

        // create mock response
        Mock<HttpResponseBase> mockResponse = new Mock<HttpResponseBase>();
        mockResponse.Setup(m => m.ApplyAppPathModifier(It.IsAny<string>())).Returns<string>(s => s);

        // create the mock context, using the request and response
        Mock<HttpContextBase> mockContext = new Mock<HttpContextBase>();
        mockContext.Setup(m => m.Request).Returns(mockRequest.Object);
        mockContext.Setup(m => m.Response).Returns(mockResponse.Object);

        // return the mock context object
        return mockContext.Object;
    }

然后您需要一个额外的辅助方法,让您指定要测试的 URL 和预期的段变量以及一个用于其他变量的对象。

        private void TestRouteMatch(string url, string controller, string action, 
        object routeProperties = null, string httpMethod = "GET")
    {
        // arrange
        RouteCollection routes = new RouteCollection();
        // loading the defined routes about the Route-Config
        RouteConfig.RegisterRoutes(routes);
        RouteData result = routes.GetRouteData(CreateHttpContext(url, httpMethod));

        // assert
        Assert.IsNotNull(result);
        // here you can check your properties (controller, action, routeProperties) with the result
        Assert.IsTrue(.....);
    }

您不需要在测试方法中定义您的路由,因为它们是使用 RouteConfig 类中的 RegisterRoutes 方法直接加载的。

入站 URL 匹配工作的机制。

GetRouteData(HttpContextBase httpContext)

referencesource.microsoft

框架为每个路由表条目调用此方法,直到其中一个返回非空值。

您必须以这种方式调用辅助方法作为示例

    [TestMethod]
    public void TestIncomingRoutes() {
        // check for the URL that is hoped for
        TestRouteMatch("~/Home/Index", "Home", "Index");
    }

如上例所示,该方法检查您期望的 URL,调用 Home Controller 中的 Index 操作。您必须在 URL 前加上波浪号 (~),这是 ASP.NET Framework 将 URL 呈现给路由系统的方式。

根据 Adam Freeman 的Pro ASP.NET MVC 5一书,我可以将它推荐给每个 ASP.NET MVC 开发人员!

关于asp.net-mvc - 测试 MVC Controller 方法的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39600717/

相关文章:

jquery - 所有文本字段中的值计算时出现问题

testing - 如何滚动 PreferenceScreen?

Angular 11 - RouterModule - 'router-outlet' 不是已知元素

c# - ASP.NET MVC 中等效的命名空间?

asp.net-mvc - 使用 jQuery.post 将多个参数发布到 MVC Controller

c# - IHttpActionResult 方法无法返回 NotFound()

ruby - 测试 Ruby TCPSocket 服务器

javascript - Angular - router.navigate() 不重定向到目标页面

asp.net-mvc - 已经定义了一个名为 'Create' 的成员,具有相同的参数类型

php - 如何在 Codeception 中编写可重用的方法/操作?