Search code examples
c#asp.net-mvcunit-testingtelerikjustmock

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


I am trying to Asp.NET MVC Admin area route unite test with telerik just mock lite.But I cant test.

Here is My Trying Code:

        [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");

    }

When var context = Mock.Create<HttpContext>(); just mock tells this error

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.

So How can I do area registration route unit test with telerik just mock lite? How can I Solve this issue?

Thanks a lot.


Solution

  • HttpContext is something you can't mock. The data it contains is specific to a particular request. So in order to run a test using HttpContext, you'll have actually run your application in an environment that you can make requests to.

    Instead, you'll need to use a third-party tool like MvcRouteTest (https://github.com/AnthonySteele/MvcRouteTester). It's easy to use, and most importantly, you can run the tests without running your app.

    [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());
    }
    

    This tests both the area registration and the route for the /admin URL (some would argue that it should be broken into two tests). It assumes that your AdminAreaRegistration's RegisterArea() method builds the default route with the default controller set as Home:

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