MvcContrib Test Helper problem
I am using MVC2 with MvcContrib HelpTester.
I have problem with testing Controllers which are in Areas.
Here is my Test class :
[TestFixture]
public class RouteTests
{
[TestFixtureSetUp]
public void Setup()
{
RouteTable.Routes.Clear();
MvcApplication.RegisterRoutes(RouteTable.Routes);
}
[Test]
public void RootMatchesHome()
{
"~/".ShouldMapTo<TradersSite.Controllers.HomeController>(x => x.Index());
}
[Test]
public void AdminProductShouldMapToIndex()
{
"~/Admin/Produit/".ShouldMapTo<TradersSite.Areas.Admin.Controllers.ProductController>(x => x.Index());
}
Here's the action Index from my ProductController in the Admin Area :
public ActionResult Index(int? page)
{
int pageSize = 10;
int startIndex = page.GetValueOrDefault() * pageSize;
var products = _productRepository.GetAllProducts()
.Skip(startIndex)
.Take(pageSize);
return View("Index", products);
}
Here is the route map in my AdminAreaRefistration :
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
Finally here is the message I get back from MbUnit :
[fixture-setup] success [failure] RouteTests.AdminProductShouldMapToIndex TestCase 'RouteTests.AdminProductShouldMapToIndex' failed: Expected Product but was Admin MvcContrib.TestHelper.AssertionExcep开发者_Go百科tion Message: Expected Product but was Admin Source: MvcContrib.TestHelper StackTrace: RouteTests.cs(44,0): at CBL.Traders.ControllerTests.RouteTests.AdminProductShouldMapToIndex()
Your area routes aren't being registered in the setup. Since you're just calling RegisterRoutes, which (by default) doesn't register areas, it's getting missed.
You can either figure a way to call AreaRegistration.RegisterAllAreas() directly (which typically gets called on app start, or you need to manually register each area you want to test. In your case, the following would work:
public void Setup()
{
RouteTable.Routes.Clear();
var adminArea = new AdminAreaRegistration();
var context = new AreaRegistrationContext("Default", RouteTable.Routes);
adminArea.RegisterArea(context);
MvcApplication.RegisterRoutes(RouteTable.Routes);
}
精彩评论