-->

Issue with MvcContrib TestHelper Fluent Route Test

2019-02-27 11:49发布

问题:

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.