user279993
user279993

Reputation: 135

MVCContrib Testing Route with Areas

I am using MVC 2 with Areas. To test routing, I am using MvcContrib.

This is the testing code:

[Test]
public void Home()
{
    MvcApplication.RegisterRoutes(RouteTable.Routes);
    "~/".ShouldMapTo<HomeController>(x => x.Login("Nps"));
}

I am not sure how to call routing definition that are stored in Areas. Calling AreaRegistration.RegisterAllAreas() is not an option as it gives an exception.

Thanks Revin

Upvotes: 4

Views: 1285

Answers (3)

Paul
Paul

Reputation: 36349

For a unit test, perhaps it's best to just do the one area. But for an integration test, you'd want to test all the routes in the context, imo.

Upvotes: 0

Laurent Kemp&#233;
Laurent Kemp&#233;

Reputation: 761

This is the way I do it which works for me

[Test]
public void VerifyRouteMapFor_Test_Area_TestController()
{
    RouteTable.Routes.Clear();

    var testAreaRegistration = new testAreaRegistration();
    testAreaRegistration.RegisterArea(new AreaRegistrationContext(testAreaRegistration.AreaName, RouteTable.Routes));

    "~/test/index".ShouldMapTo<testController>(x => x.Index());
}

Upvotes: 5

Eric Hexter
Eric Hexter

Reputation: 543

Rather than calling RegisterAllAreas, you should call the AreaRegistration for that area you are testing. The RegisterAllAreas scans all the loaded assemblies and as a result does too much for a test. I would manually setup the test. If it still throughs and exception post it here or to the mvccontrib mailing list. I am sure that there are some cases where the TestHelper needs to be updated to support areas better. We have not added any specific area support to the test helpers yet.

Upvotes: 1

Related Questions