Setup multiple expectations on a method in Rhino.Mocks

1.5k Views Asked by At

What's the correct way to setup multiple expectations on a method if you need a different return value for each consecutive invocation in Rhino.Mocks V3.6?

The following code used to work in V3.5 but fails in V3.6.

public void Test()
{
    var mocks = new MockRepository();

    var process = mocks.DynamicMock<IProcess>();
    Expect.Call(process.Run()).Return(1);
    Expect.Call(process.Run()).Return(2);

    mocks.ReplayAll();

    Assert.That(process.Run(), Is.EqualTo(1));
    Assert.That(process.Run(), Is.EqualTo(2));

    mocks.VerifyAll();
}

public interface IProcess
{
    int Run();
}
1

There are 1 best solutions below

3
On

I'm not sure if this is the correct way - but it works :)

var queue = new Queue<int>(new [] {1, 2, 3});
var mockObject = MockRepository.GenerateMock<IdGenerator>();
mockObject.Expect(calc => calc.GetNext())
          .Do( (Func<int>) queue.Dequeue);

Console.Out.WriteLine(mockObject.GetNext()); // returns 1
Console.Out.WriteLine(mockObject.GetNext()); // returns 2
Console.Out.WriteLine(mockObject.GetNext()); // returns 3

The last time I needed this I created an extension method (although for Moq). Worked out pretty well for me..

_mockClock.Setup(clock => clock.GetCurrentTime())
          .ReturnsNextValueFrom(transitionTimestamps);