问题
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