如何在RhinoMock中模拟RouteData?
我必须嘲笑这个。
(RouteData.Values["id"].Tostring()!=null)
谢谢
答案 0 :(得分:1)
鉴于RouteData的类型为IRouteData,您可以像这样模拟它:
[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"));
}
答案 1 :(得分:0)
经过大量的谷歌搜索,我得到了: 在班级声明
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);
``