ASP.NET MVC: Mock controller.Url.Action

前端 未结 5 796
醉话见心
醉话见心 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:33

    A cleaner way to do this is just use Moq(or any other framework you like) to Mock UrlHelper itself

    var controller = new OrdersController();
    var UrlHelperMock = new Mock();
    
    controller.Url = UrlHelperMock.Object;
    
    UrlHelperMock.Setup(x => x.Action("Action", "Controller", new {parem = "test"})).Returns("testUrl");
    
    var url = controller.Url.Action("Action", "Controller", new {parem = "test"});
    assert.areEqual("/Controller/Action/?parem=test",url);
    

    clean and simple.

提交回复
热议问题