为什么我的 PartialMock 方法忽略了我的期望
我有一个用作 PartialMock 的基类,例如
1 IContextManager contextManager = mocks.StrictMock<IContextManager>();
2 target = mocks.PartialMock<EnumerationServiceBase>(new object[] { contextManager });
3 Expect.Call(delegate { contextManager.RemoveContext(guid); });
4 mocks.ReplayAll();
5 actual = target.ReleaseOp(request);
target.ReleaseOp(request) 调用了 contextManager.RemoveContext 方法,我在第 3 行设置了该方法的期望,但我仍然得到以下内容错误
Rhino.Mocks.Exceptions.ExpectationViolationException: IContextManager.RemoveContext("e04c757b-8b70-4294-b133-94fd6b52ba04");预期#0,实际#1。
这是第一个不起作用的测试(其他 45 个左右都很好),但这也是第一个使用 A)部分模拟和 B)返回 void 的模拟方法的测试。有什么想法吗?
I've got a base class that I'm using as a PartialMock as such
1 IContextManager contextManager = mocks.StrictMock<IContextManager>();
2 target = mocks.PartialMock<EnumerationServiceBase>(new object[] { contextManager });
3 Expect.Call(delegate { contextManager.RemoveContext(guid); });
4 mocks.ReplayAll();
5 actual = target.ReleaseOp(request);
target.ReleaseOp(request) has a call to the contextManager.RemoveContext method which I've set an expectation for on line 3, but I still get the following error
Rhino.Mocks.Exceptions.ExpectationViolationException: IContextManager.RemoveContext("e04c757b-8b70-4294-b133-94fd6b52ba04"); Expected #0, Actual #1.
This is the first test in which this hasn't worked (the other 45 or so are fine), but this is also the first one to use A) a partial mock, and B) a mocked method that returns void. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
A) PartialMock 意味着Rhino 只会在有预期的情况下拦截方法调用。我认为你在这里的用法很好。
B) void 方法也不应该成为问题。
最有可能的是,您的问题出在您的期望中:
您期望中的
guid
需要与target
传入的 guid 实例相同。试试这个:
如果它有效,您可以相当确定您的测试 guid 和您的类中使用的实际 guid 不匹配。
A) PartialMock means Rhino will intercept method calls only if it has an expectation on it. I think your usage here is fine.
B) Void methods shouldn't be a problem either.
Most likely, your problem is in your expectation:
The
guid
in your expectation needs to be the same instance as the guid passed in bytarget
.Try this:
If it works, you can be fairly sure your test guid and actual guid used in your class don't match.