Issue with MvcContrib TestHelper Fluent Route Testing and Specific HttpVerbs
Asked Answered
L

1

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

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?

Largent answered 11/11, 2010 at 17:17 Comment(0)
D
0

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.

Dambrosio answered 11/11, 2010 at 22:39 Comment(1)
Thanks for the response Chris. It is not related to my routes in this case. I can verify that by changing the name of my POST action to say "IdentifyUserPost" and then trying to access it via a GET request in the browser. That fails as expected, but if I alter my test to now route to "~/public/registration/useridentification/identifyuserpost" AND change it to use the GET verb, I still see a passing test. It seems that is more a result of the action not executing, just some level of route verification. I'm looking into the source of MvcContrib to learn more...Largent

© 2022 - 2024 — McMap. All rights reserved.