Issue with MvcContrib TestHelper Fluent Route Testing and Specific HttpVerbs

ぐ巨炮叔叔 提交于 2020-01-11 10:48:42

问题


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

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

Even though the POST HttpVerb is specified in my test, it always routes to the HttpGet method. I can even comment out the action accepting HttpPost in my controller and still have the test pass!

Is there something I'm missing here?


回答1:


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

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.



来源:https://stackoverflow.com/questions/4157161/issue-with-mvccontrib-testhelper-fluent-route-testing-and-specific-httpverbs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!