簡體   English   中英

使用Telerik Just Mock Lite進行Asp.NET MVC區域注冊路由單元測試

[英]Asp.NET MVC Arearegistration Route Unit Test With Telerik Just Mock Lite

我正在嘗試使用telerik只是模擬lite.Asp.NET MVC管理員區域路由聯合測試。但是我無法測試。

這是我的嘗試代碼:

        [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.

那么,如何僅用lite進行Telerik的區域注冊路由單元測試? 我該如何解決這個問題?

非常感謝。

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的路由(有些人認為應該將其分為兩個測試)。 假定您的AdminAreaRegistrationRegisterArea()方法使用默認控制器設置為Home來構建默認路由:

public override void RegisterArea(AreaRegistrationContext context) 
{
    context.MapRoute(
        "Admin_default",
        "Admin/{controller}/{action}/{id}",
        new { controller="home", action = "Index", id = UrlParameter.Optional }
    );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM