我正在尝试Asp.NET MVC管理区路由联合测试与telerik只是模拟lite.But,我不能测试。
这是我的尝试代码:
[TestMethod]
public void AdminRouteUrlIsRoutedToHomeAndIndex()
{
//spts.saglik.gov.tr/admin
//create route collection
var routes = new RouteCollection();
var areaRegistration = new AdminAreaRegistration();
Assert.AreEqual("Admin",areaRegistration.AreaName);
// Get an AreaRegistrationContext for my class. Give it an empty RouteCollection
var areaRegistrationContext = new AreaRegistrationContext(areaRegistration.AreaName, routes);
areaRegistration.RegisterArea(areaRegistrationContext);
// Mock up an HttpContext object with my test path (using Moq)
var context = Mock.Create<HttpContext>();
context.Arrange(c=>c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/Admin");
// Get the RouteData based on the HttpContext
var routeData = routes.GetRouteData(context.Request.RequestContext.HttpContext);
//assert has route
Assert.IsNotNull(routeData,"route config");
}当var context = Mock.Create<HttpContext>();只是模拟告诉这个错误时
Telerik.JustMock.Core.ElevatedMockingException: Cannot mock 'System.Web.HttpContext'. JustMock Lite can only mock interface members, virtual/abstract members in non-sealed classes, delegates and all members on classes derived from MarshalByRefObject on instances created with Mock.Create or Mock.CreateLike. For any other scenario you need to use the full version of JustMock.
那么,我如何使用telerik进行区域注册路由单元测试?我怎样才能解决这个问题?
非常感谢。
发布于 2016-05-09 18:17:51
HttpContext是你不能嘲笑的东西。它包含的数据是特定于特定请求的。因此,为了使用HttpContext运行测试,您将实际在一个可以发出请求的环境中运行应用程序。
相反,您需要使用第三方工具,比如MvcRouteTest (https://github.com/AnthonySteele/MvcRouteTester)。它很容易使用,而且最重要的是,您可以在不运行应用程序的情况下运行测试。
[TestMethod]
public void AdminRouteUrlIsRoutedToHomeAndIndex()
{
var routes = new RouteCollection();
var areaRegistration = new AdminAreaRegistration();
Assert.AreEqual("Admin", areaRegistration.AreaName);
var areaRegistrationContext = new AreaRegistrationContext(areaRegistration.AreaName, routes);
areaRegistration.RegisterArea(areaRegistrationContext);
routes.ShouldMap("/admin").To<HomeController>(r => r.Index());
}这既测试了区域注册,也测试了/admin URL的路径(有些人认为应该将其分成两个测试)。它假设您的AdminAreaRegistration的RegisterArea()方法构建默认路由,默认控制器设置为Home:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller="home", action = "Index", id = UrlParameter.Optional }
);
}https://stackoverflow.com/questions/37117401
复制相似问题