2011-06-23 69 views

回答

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")); 


    } 
+0

感謝Morten。我已經使用了另一種方法,稍有不同。我現在會發布。 –

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); 

`