ASP.NET MVC: Mock controller.Url.Action

前端 未结 5 802
醉话见心
醉话见心 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条回答
  •  猫巷女王i
    2020-12-14 02:22

    Here is another way to solve the problem with NSubstitute. Hope, it helps someone.

    // _accountController is the controller that we try to test
    var urlHelper = Substitute.For();
    urlHelper.Action(Arg.Any(), Arg.Any()).Returns("/test_Controller/test_action");
    
    var context = Substitute.For();
    _accountController.Url = urlHelper;
    _accountController.ControllerContext = new ControllerContext(context, new RouteData(), _accountController);
    
        

    提交回复
    热议问题