简体   繁体   English

ASP.net MVC单元测试中的模拟路由约束

[英]Mocking Route Constraints in ASP.net MVC Unit test

I am trying to use Moq to test a route with a constraint. 我正在尝试使用Moq来测试具有约束的路线。 I am not testing the constraint, but the route and want to just return true for when the route is tested as part of the route test. 我不是在测试约束,而是在测试路由,并且只想在测试路由作为路由测试的一部分时返回true。 The following code is an attempt to mock the constraint in the test setting it to always return true. 以下代码是尝试在测试中模拟约束以使其始终返回true的尝试。

Mock<SomeRouteConstraint> routeConstraint = new Mock<SomeRouteConstraint>();
routeConstraint.Setup(tC => tC.Match(It.IsAny<HttpContextBase>(), It.IsAny<Route>(), It.IsAny<string>(), It.IsAny<RouteValueDictionary>(), It.IsAny<RouteDirection>())).Returns(true);

context.Setup(ctx => ctx.Request.AppRelativeCurrentExecutionFilePath).Returns("someurl/thatmaps/toavalue");

RouteData routeData = routes.GetRouteData(context.Object);

Assert.IsNotNull(routeData, "Did not find the route");

The code executes fine, but the actual constraint is tested instead of just returning true per the Mock. 该代码可以很好地执行,但是实际的约束已经过测试,而不仅仅是根据Mock返回true。

Here is the sample route: 这是示例路线:

            Routes.Add("someroutewithconstraint",
            new LowercaseRoute("{someurl}/{thatmaps}/{toavalue}",
            new { controller = "SomeController", action = "SomeAction" },
            new {  
                  someConstraint = new SomeRouteConstraint()
            },
            new LowercaseRouteHandler());

The real question here is actually how to use the Mocked route constraint instead of the real constraint. 真正的问题实际上是如何使用模拟路由约束而不是实际约束。 Is there something on the context that allows Moq to override route contraints? 在上下文中是否存在允许Moq覆盖路线约束的内容?

Solved this by creating a class to attach constrain names to http items collection. 通过创建一个将约束名称附加到http项目集合的类来解决此问题。 Then we override the ProcessConstraint method in our own Route and check the collection for constraints to skip (each one can be set to true or false). 然后,我们在自己的Route中重写ProcessConstraint方法,并检查集合中要跳过的约束(每个约束可以设置为true或false)。 Remember we are testing the route not the constraint. 请记住,我们是在测试路线而不是约束。

    protected override bool ProcessConstraint(System.Web.HttpContextBase httpContext, object constraint, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        bool retValue = false;
        string contraintName = constraint.GetType().Name;

        if (httpContext.Items != null && httpContext.Items.Contains(contraintName))
        {
            //Inspect list of constraints to override
            if (httpContext.Items.Contains(constraint.GetType().Name))
                bool.TryParse(httpContext.Items[constraint.GetType().Name].ToString(), out retValue);
        }
        else
        {
            retValue = base.ProcessConstraint(httpContext, constraint, parameterName, DecodeValues(values), routeDirection);
        }

        return retValue;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM