gpt4 book ai didi

.net - 在 Rhino Mock 中,我如何 mock 一个属性使其多次成为 “call”?

转载 作者:行者123 更新时间:2023-12-02 02:21:02 25 4
gpt4 key购买 nike

这应该很容易,所以我一定遗漏了一些东西(很可能这是我第一次使用 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;

最佳答案

尝试生成一个 Mock

MockRepository.GenerateMock<IHelm>()

而不是 stub :

Stub<IHelm>()

The difference between stubs and mocks (Rhino Mocks 在线文档)

A mock is an object that we can set expectations on, and which will verify that the expected actions have indeed occurred. A stub is an object that you use in order to pass to the code under test. You can setup expectations on it, so it would act in certain ways, but those expectations will never be verified. A stub's properties will automatically behave like normal properties, and you can't set expectations on them

关于.net - 在 Rhino Mock 中,我如何 mock 一个属性使其多次成为 “call”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8155594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com