Mock RouteData in RhinoMocks-MVC3

て烟熏妆下的殇ゞ 提交于 2020-01-06 11:47:36

问题


How to Mock RouteData in RhinoMock?

I have to Mock this.

(RouteData.Values["id"].Tostring()!=null)

Thank you


回答1:


Given that RouteData is of type IRouteData you can mock it like this:

    [Test]
    public void TestMockingConcreteClass()
    {
        MockRepository mockRepository = new MockRepository();

        RouteData routeData = mockRepository.Stub<RouteData>();

        routeData.Stub(r => r.Values["id"]).Return("XXX");

        mockRepository.ReplayAll();

        Assert.That(routeData.Values["id"].ToString(), Is.EqualTo("XXX"));


    }



回答2:


After lots of googling I got this to : Declared at the Class Level

     HttpContextBase httpContextMock;
      ControllerBase controllerMock;

 In the Constructor
         mockRepository = new MockRepository();
         httpContextMock = mockRepository.DynamicMock<HttpContextBase>();
        controllerMock = mockRepository.DynamicMock<ControllerBase>();

      In The Test Method:
       var routeData = new RouteData();
        routeData.Values.Add("id", "Value");

      absenceController.ControllerContext = new ControllerContext(httpContextMock,    routeData, controllerMock);

``



来源:https://stackoverflow.com/questions/6452626/mock-routedata-in-rhinomocks-mvc3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!