This morning we were looking for the usage of mocking
"out" parameters and we found a solution, but we had to
look into the source code of NMock2 and do some
research and tests until we found the solution.
The Method to mock:
int GetRevocation(string plate, out bool found);
The solution:
Mockery mockery = new Mockery();
IServiceAgent mock = mockery.NewMock<IServiceAgent>();
Expect.Once.On(mock).Method("GetRevocation").
With(Is.EqualTo("ZG123"), Is.Out).Will(Return.Value(0),
new NMock2.Actions.SetNamedParameterAction("found", true));
mockery.VerifyAllExpectationsHaveBeenMet();
Greetings from Switzerland
Thomas
Logged In: NO
It's not working for me. I'm getting:
at
System.Runtime.Remoting.Proxies.RealProxy.ValidateReturnArg
(Object arg, Type paramType)
at
System.Runtime.Remoting.Proxies.RealProxy.PropagateOutParame
ters(IMessage msg, Object[] outArgs, Object returnValue)
at
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessag
e(IMessage reqMsg, IMessage retMsg)
at
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke
(MessageData& msgData, Int32 type)
Logged In: YES
user_id=1140914
Here is the full source code that works for me with NMock2
version 1.0.2313.18049 and NUnit version 2.2.7.0:
using System;
using NUnit.Framework;
using NMock2;
namespace BusinessLogic.Test
{
[TestFixture]
public class OutParameterTest
{
public interface IServiceAgent
{
int GetRevocation(string plate, out bool found);
}
public class MyClassToTest
{
IServiceAgent m_serviceAgent;
public MyClassToTest(IServiceAgent serviceAgent)
{
m_serviceAgent = serviceAgent;
}
public void MyMethodToTest()
{
bool found;
int res =
m_serviceAgent.GetRevocation("ZG123", out found);
// .... more code
}
}
[Test]
public void OutParameters()
{
Mockery mockery = new Mockery();
IServiceAgent mock =
mockery.NewMock<IServiceAgent>();
Expect.Once.On(mock).Method("GetRevocation").
With(Is.EqualTo("ZG123"),
Is.Out).Will(Return.Value(0),
new
NMock2.Actions.SetNamedParameterAction("found", true));
new MyClassToTest(mock).MyMethodToTest();
mockery.VerifyAllExpectationsHaveBeenMet();
}
}
}