MvcContrib Test Helper Task

I am using MVC2 with the MtcContrib HelpTester.

I have a problem with testing controllers that 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 is the action pointer 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 received from MbUnit:

Success

[fixture-setup] [crash] RouteTests.AdminProductShouldMapToIndex TestCase 'RouteTests.AdminProductShouldMapToIndex' failed: expected product, but there was Admin MvcContrib.TestHelper.AssertionException Message: Expected product, but there was Admin Source: Mtcestc route (44.0): in CBL.Traders.ControllerTests.RouteTests.AdminProductShouldMapToIndex ()

+4
source share
1 answer

Your route routes are not recorded in the settings. Since you simply call RegisterRoutes, which (by default) does not register areas, it is skipped.

You can either specify the method of calling AreaRegistration.RegisterAllAreas () directly (which is usually called when the application starts, or you need to manually register each area that you want to check). In your case, the following will 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); } 
+4
source

Source: https://habr.com/ru/post/1308677/


All Articles