NMock and out parameters

While trying to create a unit test the other day I came across the situation where my mock object needed to return a value via an Out parameter. A quick google turned up this post over at dev:ices.

I thought that had answered all my questions but when I tried it I kept getting a very unhelpful error:

—— Test started: Assembly: Tests.dll ——

TestCase ‘Tests.BatchOrderMessageHandlerTests.TestNMock’
failed: NMock2.Internal.ExpectationException : unexpected invocation of foo.DoFoo(, , out)
Expected:
1 time: foo.DoFoo(equal to , equal to , equal to ), will set c=, return [called 0 times]
at NMock2.Mockery.FailUnexpectedInvocation(Invocation invocation)
at NMock2.Mockery.Dispatch(Invocation invocation)
at NMock2.Mockery.MockObject.Invoke(Invocation invocation)
at NMock2.Monitoring.Invoker.Invoke(Invocation invocation)
at NMock2.Monitoring.ProxiedObjectIdentity.Invoke(Invocation invocation)
at NMock2.Monitoring.ProxyInvokableAdapter.Invoke(IMessage msg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at Tests.BatchOrderMessageHandlerTests.IFoo.DoFoo(Int32 a, Int32 b, Int32& c)
C:svnGLGParnters.TradeIdeasPositionManagersrcTestsBatchOrderMessageHandlerTests.cs(136,0): at Tests.BatchOrderMessageHandlerTests.TestNMock()

0 passed, 1 failed, 0 skipped, took 0.77 seconds.

A bit confused I quickly whipped up a trivial test as my unit test was a bit complicated and returned an enum as the Out parameter. My hypothesis was that NMocks couldn’t return an enum as an Out parameter.

public interface IFoo
{
int DoFoo(int a, int b, out int c);
}

[Test]
public void TestNMock()
{
IFoo foo = _mockery.NewMock();

Expect.Once.On(foo).Method(“DoFoo”)
.With(1, 1, Is.Out)
.Will(new SetNamedParameterAction(“c”, 2), Return.Value(1));

int result = 0;
int a = foo.DoFoo(1, 1, out result);

_mockery.VerifyAllExpectationsHaveBeenMet();
Assert.AreEqual(2, result);
}

This also failed so I checked what version of NMocks I was using. My NMock2.dll assembly had a revision number of 1.0.2313.18049. I downloaded the latest binaries from the NMock website. Checking the version number of the latest build shows it to be 2.0.0.44.

Rerunning the unit tests against this version of the NMock library everything now works!

So, if you’re having trouble with out parameters in your NMock mocks, upgrade your build version.

2 thoughts on “NMock and out parameters

  1. I’m running into a similar problem programming in Visual Basic making use of parameters passed By Reference. The difference is that I’m already making use of the 2.0.0.44 binary for NMock2.dll.

    This seems to have been fixed quite a while ago for your application, it makes me wonder if it isn’t also fixed for mine and there is some other factor at play that is breaking my implementation.

    In any case, thanks for sharing.

    Like

  2. It turns out my issue was not with the parameters that were passed by reference. I was assuming that mock was smart enough to expect the parameter data types defined in the Interface.

    As such, I was setting expectations that were all implicitly Integer in type like so:
    Dim byrefParam as Integer = 2
    Expect.Once.On(mockFoo).Method(“Bar”).With(1, byrefParam).Will(New Actions.SetNamedParameterAction(“refParam2”, 3))

    Where the mocked interface was define like this:
    Interface IFoo
    Sub Bar(ByVal param1 as UShort, ByRef refParam2 as Integer)
    End Interface

    When my module under test made calls to the mocked funtion, it was passing in the proper data types (eg. UShort) and this produced an error similar to the one you describe.

    Explicitly type casting the expected parameters using ‘CUShort(1)’ or ‘1US’ sorted everything out:
    Expect.Once.On(mockFoo).Method(“Bar”).With(1US, byrefParam).Will(New Actions.SetNamedParameterAction(“refParam2”, 3))

    Happy to have this figured… although it seems not to have been related to you original problem at all. =)

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s