我嘗試了RhinoMocks(只是剛剛開始,請溫柔!),並很困惑,當輸入欄爲什麼如果我用這個設置:RhinoMocks - 無法投Foo類型使用非靜態庫
var mockRepos = new MockRepository();
var mockServiceProvider = mockRepos.DynamicMock<IServiceProvider>(null);
var mockContext = mockRepos.DynamicMock<IPluginExecutionContext>(null);
mockServiceProvider.Expect(x => x.GetService(typeof(IPluginExecutionContext))).Return(mockContext);
var someSampleClass = new SomeClassOrOther(mockServiceProvider);
在SomeClassOrOther某一點,該方法GetService
被稱爲
var context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
,並導致異常:
Unable to cast object of type 'Castle.Proxies.ObjectProxyd0bf4b879a6341bbba3478cf1189d621' to type 'Microsoft.Xrm.Sdk.IPluginExecutionContext'.
但是,如果我使用:
var mockServiceProvider = MockRepository.GenerateMock<IServiceProvider>(null);
var mockContext = MockRepository.GenerateMock<IPluginExecutionContext>(null);
mockServiceProvider.Expect(x => x.GetService(typeof(IPluginExecutionContext))).Return(mockContext);
即靜態MockRepository.GenerateMock
並沒有錯誤執行。
我在哪裏錯了?
啊,我用期待。我應該使用存根嗎? – glosrob
如果您稍後未驗證期望,請使用存根。 –