本文介绍了问题与MvcContrib TestHelper流利的路线测试和具体HttpVerbs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用MvcContrib TestHelper流畅的路线测试API,但是我看到奇怪的行为。该.WithMethod(HttpVerb)扩展方法似乎并不像预期的那样被执行。这里是我的控制器显示,接受不同HttpVerbs(2)操作(同名):

I'm attempting to use the MvcContrib TestHelper fluent route testing API, but I'm seeing odd behavior. The .WithMethod(HttpVerb) extension method does not seem to be executing as expected. Here's my controller showing (2) actions (identically named) that accept different HttpVerbs:

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

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

这是应该映射到与[HttpPost]属性的动作测试:

And here is the test that should map to the action with the [HttpPost] attribute:

MvcApplication.RegisterRoutes(RouteTable.Routes);

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

虽然POST HttpVerb是在我的测试中指定,它总是路由到HTTPGET方法。的我甚至可以注释掉行动接受HttpPost在我的控制器,并仍然通过测试!

有我丢失的东西在这里?

Is there something I'm missing here?

推荐答案

这可能与你是如何注册您的路线做。我通常创建一个类,确实只。因此,像高于任何测试之前,我要确保我适当的设置我的测试夹具。

It might have to do with how you're registering your routes. I typically create a class that does only that. So before any tests like those above, I make sure I set up my test fixture appropriately.

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

我的猜测是,既然RouteTable静态处理它们,你可能会运行到由要么不添加,不清除,或增加您的每次测试运行太多的路线问题。

My guess is that since the RouteTable handles them statically, you might be running into problems by either not adding, not clearing, or adding too many routes per your test runs.

这篇关于问题与MvcContrib TestHelper流利的路线测试和具体HttpVerbs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 13:16