【问题标题】:Issue with MvcContrib TestHelper Fluent Route Testing and Specific HttpVerbsMvcContrib TestHelper Fluent Route 测试和特定 HttpVerbs 的问题
【发布时间】:2010-11-11 17:17:12
【问题描述】:

我正在尝试使用 MvcContrib TestHelper 流畅的路由测试 API,但我看到了奇怪的行为。 .WithMethod(HttpVerb) 扩展方法似乎没有按预期执行。这是我的控制器,显示了 (2) 个接受不同 HttpVerbs 的操作(名称相同):

[HttpGet]
public ActionResult IdentifyUser()
{
    return View(new IdentifyUserViewModel());
}

[HttpPost]
public ActionResult IdentifyUser(IdentifyUserInputModel model)
{
    return null;
}

下面是应该映射到具有 [HttpPost] 属性的操作的测试:

MvcApplication.RegisterRoutes(RouteTable.Routes);

var routeData = "~/public/registration/useridentification/identifyuser"
    .WithMethod(HttpVerbs.Post)
    .ShouldMapTo<UserIdentificationController>(x => x.IdentifyUser(null));

即使在我的测试中指定了 POST HttpVerb,它也总是路由到 HttpGet 方法。 我什至可以在我的控制器中注释掉接受 HttpPost 的动作并且仍然通过测试!

这里有什么我遗漏的吗?

【问题讨论】:

    标签: asp.net-mvc-2 nunit mvccontrib-testhelper


    【解决方案1】:

    这可能与您注册路线的方式有关。我通常会创建一个仅执行此操作的类。因此,在进行上述任何测试之前,我确保我正确设置了我的测试夹具。

    [TestFixtureSetUp]
    public void TestFixtureSetUp()
    {
        RouteTable.Routes.Clear();
        new RouteConfigurator().RegisterRoutes(RouteTable.Routes);
    }
    

    我的猜测是,由于 RouteTable 静态处理它们,您可能会遇到问题,因为每次测试运行时不添加、不清除或添加太多路由。

    【讨论】:

    • 感谢克里斯的回复。在这种情况下,它与我的路线无关。我可以通过将 POST 操作的名称更改为“IdentifyUserPost”然后尝试通过浏览器中的 GET 请求访问它来验证这一点。正如预期的那样失败,但是如果我将测试更改为现在路由到“~/public/registration/useridentification/identifyuserpost”并将其更改为使用 GET 动词,我仍然会看到通过测试。似乎这更多的是未执行操作的结果,只是某种程度的路由验证。我正在研究 MvcContrib 的来源以了解更多信息...
    猜你喜欢
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    相关资源
    最近更新 更多