views:

22

answers:

1

I have an issue with testing routes using the MVCContrib Fluent route testing. The test fails yet the application recognises the routes.

Let me explain....

I have the following routes in my register (shown in order)

routes.MapRoute(
                "PurchaseUnitsPaged",
                "PurchaseUnits/Page{page}",
                new { controller = "PurchaseUnits", action = "Index", page = 1 },
                new { page = @"\d+" }
                );


routes.MapRoute(
                "PurchaseUnit",
                "PurchaseUnits/{unitname}",
                new { controller = "PurchaseUnits", action = "Show" }
            );

The routing pipeline correctly sends requests to Index for route 1 and Show for route 2.

However when I test the routing using the MVCContrib fluent classes I get a test fail for route 1.

The test is:

"~/PurchaseUnits/Page{page}".ShouldMapTo<PurchaseUnitsController>(x=> x.Index(1));

The test fails because the expectation is Index but the actual is Show.

Any ideas as to why the fluent classes aren't identifying the correct routing yet the mvc routing works in the actual application? Or failing that any suggestions about how I can tweak my test or routes to allow me to fully test?

+2  A: 

Your test should be:

"~/PurchaseUnits/Page1".ShouldMapTo<PurchaseUnitsController>(x=> x.Index(1));

The url is ~/PurchaseUnits/Page1 and not ~/PurchaseUnits/Page{page}.

Darin Dimitrov
Cheers for the answer Darin, that works.I'm slightly confused though because I test the second route mentioned in the question by using the following test"~/PurchaseUnits/{unitname}".Route().ShouldMapTo<PurchaseUnitsController>(x => x.Show("{unitname}"));
ActualAl
That's because "{unitname}" matches "{unitname}". If your original test was "~/PurchaseUnits/Page{page}".ShouldMapTo<PurchaseUnitsController>(x=> x.Index("{page}")); it (edit: WOULD NOT) have worked, but apparently Index takes an int while Show takes a string. Actually, looking at your code closer, "~/PurchaseUnits/Page{page}" does not match your first route, as the value "{page}" is not matched by "\d+", and instead would fall down to the next route PurchaseUnitsController.Show("Page{page}").
ARM