开发者

What is the right way to mock void functions in RhinoMocks?

开发者 https://www.devze.com 2023-02-02 20:25 出处:网络
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:

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.Cr开发者_运维技巧eateMock<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.


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();


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


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消