Problem with testing MvcContrib TestHelper for free route and specific HttpVerbs

I am trying to use the MvcContrib TestHelper validation API, but I am seeing strange behavior. The extension method .WithMethod (HttpVerb) does not seem to execute as expected. Here my controller shows (2) actions (equally named) that accept different HttpVerbs:

[HttpGet] public ActionResult IdentifyUser() { return View(new IdentifyUserViewModel()); } [HttpPost] public ActionResult IdentifyUser(IdentifyUserInputModel model) { return null; } 

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

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

Despite the fact that the POST HttpVerb is specified in my test, it is always routed to the HttpGet method. I can even comment on the action taking the HttpPost in my controller and still pass the test!

Am I missing something here?

+2
nunit asp.net-mvc-2
source share
1 answer

Perhaps this is due to the way you register your routes. Usually I create a class that does just that. Therefore, before any tests, such as the ones above, I make sure that I have set up my test device correctly.

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

I assume that since RouteTable handles them statically, you might run into problems without adding, clearing, or adding too many routes to your test runs.

0
source

All Articles