ASP.NET MVC: Mock controller.Url.Action

前端 未结 5 805
醉话见心
醉话见心 2020-12-14 02:03

Urls for menus in my ASP.NET MVC apps are generated from controller/actions. So, they call

controller.Url.Action(action, controller)

Now, h

5条回答
  •  被撕碎了的回忆
    2020-12-14 02:17

    Here's how you could mock UrlHelper using MvcContrib's TestControllerBuilder:

    var routes = new RouteCollection();
    MvcApplication.RegisterRoutes(routes);
    HomeController controller = CreateController();
    
    controller.HttpContext.Response
        .Stub(x => x.ApplyAppPathModifier("/Home/About"))
        .Return("/Home/About");
    
    controller.Url = new UrlHelper(
        new RequestContext(
            controller.HttpContext, new RouteData()
        ), 
        routes
    );
    var url = controller.Url.Action("About", "Home");
    Assert.IsFalse(string.IsNullOrEmpty(url));
    

提交回复
热议问题