这应该是容易的,所以我必须失去了一些东西(很可能因为这是用犀牛模拟我的第一次)在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)会使它成为一个很有用的答案。 –