【问题标题】:MVCContrib Testing Route with Areas带区域的 MVCContrib 测试路线
【发布时间】:2011-02-09 22:54:12
【问题描述】:
我正在使用带有区域的 MVC 2。为了测试路由,我使用了 MvcContrib。
这是测试代码:
[Test]
public void Home()
{
MvcApplication.RegisterRoutes(RouteTable.Routes);
"~/".ShouldMapTo<HomeController>(x => x.Login("Nps"));
}
我不确定如何调用存储在区域中的路由定义。
调用 AreaRegistration.RegisterAllAreas() 不是一个选项,因为它会给出异常。
谢谢
雷文
【问题讨论】:
标签:
unit-testing
asp.net-mvc-routing
mvccontrib
【解决方案1】:
这就是我的工作方式
[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());
}
【解决方案2】:
您应该为正在测试的区域调用 AreaRegistration,而不是调用 RegisterAllAreas。 RegisterAllAreas 扫描所有加载的程序集,因此对测试做的太多。我会手动设置测试。如果它仍然通过并且异常发布在这里或到 mvccontrib 邮件列表。我确信在某些情况下需要更新 TestHelper 以更好地支持区域。我们还没有为测试助手添加任何特定区域的支持。
【解决方案3】:
对于单元测试,最好只做一个区域。但是对于集成测试,您需要测试上下文中的所有路由,imo。