在 RhinoMocks 中模拟 void 函数的正确方法是什么?
我有这个接口,它在某些函数中返回 void,我想模拟这些函数,并想知道这样做的正确方法是什么。截至目前,我有以下问题:
var mocks = new MockRepository();
var mockedInterface = mocks.CreateMock<IMyInterface>();
Expect.Call(mockedInterface.FunctionThatReturn(param1, param2)).Return(Something);
mockedInterface.FunctionReturningVoid(param3, param4);
mocks.ReplayAll();
// Some assert and other stuff
mocks.VerifyAll();
这是正确的做法吗?我认为这看起来很奇怪,因为您没有以相同的方式处理这两个函数。我想写的是:
var mocks = new MockRepository();
var mockedInterface = mocks.CreateMock<IMyInterface>();
Expect.Call(mockedInterface.FunctionThatReturn(param1, param2)).Return(Something);
Expect.Call(mockedInterface.FunctionReturningVoid(param3, param4)); // This doesn't work.
mocks.ReplayAll();
// Some assert and other stuff
mocks.VerifyAll();
但这在第 4 行不起作用。我发现一些博客说你可以使用 lambda(或委托),例如
Expect.Call(() => mockedInterface.FunctionReturningVoid(param3, param4)); // This doesn't work.
但这似乎对我不起作用。拥有 Expect.Call 可以轻松识别模拟函数,这就是我想要它的原因。我得到的编译错误是:“无法将 lambda 表达式转换为类型‘object’,因为它不是委托类型”。
那么应该怎么做呢?
更新:添加了编译错误信息。
I have this interface that returns void in some functions that I would like to mock and wonder what is the correct way of doing so. As of now I have the following:
var mocks = new MockRepository();
var mockedInterface = mocks.CreateMock<IMyInterface>();
Expect.Call(mockedInterface.FunctionThatReturn(param1, param2)).Return(Something);
mockedInterface.FunctionReturningVoid(param3, param4);
mocks.ReplayAll();
// Some assert and other stuff
mocks.VerifyAll();
Is that the right way of doing it? I think it looks weird since you're not handling the two functions the same way. What I would like to write is:
var mocks = new MockRepository();
var mockedInterface = mocks.CreateMock<IMyInterface>();
Expect.Call(mockedInterface.FunctionThatReturn(param1, param2)).Return(Something);
Expect.Call(mockedInterface.FunctionReturningVoid(param3, param4)); // This doesn't work.
mocks.ReplayAll();
// Some assert and other stuff
mocks.VerifyAll();
But that doesn't work on row 4. I found some blog that says you can use lambdas (or delegate) like
Expect.Call(() => mockedInterface.FunctionReturningVoid(param3, param4)); // This doesn't work.
But that doesn't seem to work either for me. Having the Expect.Call
makes it easy to identify mocked functions and that is why I want it. The compile error I get is: "Cannot convert lambda expression to type 'object' because it is not a delegate type".
So how should it be done?
UPDATE: Added compile error information.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我更喜欢 AAA(排列/执行/断言)语法而不是记录/重放。它更简单并且使测试更容易阅读。你想要做的是:
I prefer the AAA (arrange/act/assert) syntax instead of record/replay. It's more straightforward and makes the tests easier to read. What you'll want to do is:
对于 void 方法,我使用匿名委托:
顺便说一句:我喜欢记录回放语法来重播和验证期望
http://www. ayende.com/Wiki/(S(j2mgwqzgkqghrs55wp2cwi45))/Comparison+of+ Different+Rhino+Mocks+syntaxes.ashx
For void methods I use anonymous delegates:
BTW: I like Record-Playback syntax for replaying and verifying expectations
http://www.ayende.com/Wiki/(S(j2mgwqzgkqghrs55wp2cwi45))/Comparison+of+different+Rhino+Mocks+syntaxes.ashx
不知道如何在 AAA 模式中测试 void 方法,我在嘲笑 void 时也遇到了麻烦。但是,在过去,我使用“录制和播放”风格,这应该可行。
例子:
Not sure how to test void method in AAA pattern, I was also having trouble mocking void. However, in the past, I use the Record and Playback style, and that should work.
Example: