這應該是容易的,所以我必須失去了一些東西(很可能因爲這是用犀牛模擬我的第一次)在Rhino Mock中,我如何嘲笑一個屬性可以多次「調用」?
我只希望我的代碼能夠調用helm.CurrentEnterprise
任意次數,而是我得到:
System.InvalidOperationException occurred
Message=Previous method 'IHelm.get_CurrentEnterprise();' requires a return value or an exception to throw.
Source=Rhino.Mocks
StackTrace:
at Rhino.Mocks.Impl.RecordMockState.AssertPreviousMethodIsClose()
at Rhino.Mocks.Impl.RecordMockState.MethodCall(IInvocation invocation, MethodInfo method, Object[] args)
at Rhino.Mocks.MockRepository.MethodCall(IInvocation invocation, Object proxy, MethodInfo method, Object[] args)
at Rhino.Mocks.Impl.RhinoInterceptor.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at IHelmProxy44ecadd4f07244fd96c5849febe94a58.get_CurrentEnterprise()
at KSS.PS3.Testing.UnitTests.ModelOptions.RuleGroupTreeViewTest.AsUsedByRuleGroupModalOptionEditor() in D:\dev\5.0.0\main\Application\Testing\Tests\UnitTests\ModelOptions\RuleGroupTreeView.cs:line 54
InnerException:
這是我的代碼:
MockRepository mocks = new MockRepository();
IHelm helm = mocks.Stub<IHelm>();
helm.Stub(x => x.CurrentEnterprise).Return(enterprise).Repeat.Any();
var a2 = helm.CurrentEnterprise;
var a2a = helm.CurrentEnterprise; // <- the exception comes from here
var a2aa = helm.CurrentEnterprise;
你可以嘗試一樣,但使用'MockRepository.GenerateMock()'而不是'存根()'? –
sll
@sll,thanks MockRepository.GenerateMock();工作。請你可以把你的評論變成答案,這樣我就可以接受它,解釋GenerateMock的工作方式,但不是存根(stub)會使它成爲一個很有用的答案。 –