在 RhinoMocks 中模拟 void 函数的正确方法是什么?

发布于 2024-10-10 14:36:17 字数 1155 浏览 4 评论 0原文

我有这个接口,它在某些函数中返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

糖粟与秋泊 2024-10-17 14:36:17

我更喜欢 AAA(排列/执行/断言)语法而不是记录/重放。它更简单并且使测试更容易阅读。你想要做的是:

// arrange
var mock = MockRepository.GenerateMock<IMyInterface>
mock.Expect(i => i.FunctionThatReturnSomething(param1, param2)).Return("hello");
mock.Expect(i => i.FunctionThatReturnVoid(param3, param4));
// set up other stuff for your code (like whatever code depends on IMyInterface)
var foo = new Foo(mock);

// act
foo.DoSomething();

// assert
mock.VerifyAll();

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:

// arrange
var mock = MockRepository.GenerateMock<IMyInterface>
mock.Expect(i => i.FunctionThatReturnSomething(param1, param2)).Return("hello");
mock.Expect(i => i.FunctionThatReturnVoid(param3, param4));
// set up other stuff for your code (like whatever code depends on IMyInterface)
var foo = new Foo(mock);

// act
foo.DoSomething();

// assert
mock.VerifyAll();
末が日狂欢 2024-10-17 14:36:17

对于 void 方法,我使用匿名委托:

Expect.Call(delegate { mockedInterface.FunctionReturningVoid(param3, param4); })

顺便说一句:我喜欢记录回放语法来重播和验证期望
http://www. ayende.com/Wiki/(S(j2mgwqzgkqghrs55wp2cwi45))/Comparison+of+ Different+Rhino+Mocks+syntaxes.ashx

For void methods I use anonymous delegates:

Expect.Call(delegate { mockedInterface.FunctionReturningVoid(param3, param4); })

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

我爱人 2024-10-17 14:36:17

不知道如何在 AAA 模式中测试 void 方法,我在嘲笑 void 时也遇到了麻烦。但是,在过去,我使用“录制和播放”风格,这应该可行。

例子:

private MockRepository m_mocks = new MockRepository();
private IXGateManager xGateManager = m_mocks.DynamicMock<IXGateManager>();

using (m_mocks.Record())
{
    xGateManager.SendXGateMessage(null, null);
    LastCall.IgnoreArguments().Repeat.Once();
}

using (m_mocks.Playback())
{
    //... execute your test
}

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:

private MockRepository m_mocks = new MockRepository();
private IXGateManager xGateManager = m_mocks.DynamicMock<IXGateManager>();

using (m_mocks.Record())
{
    xGateManager.SendXGateMessage(null, null);
    LastCall.IgnoreArguments().Repeat.Once();
}

using (m_mocks.Playback())
{
    //... execute your test
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文