开发者

Setup multiple expectations on a method in Rhino.Mocks

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

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);
    E开发者_JS百科xpect.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();
}


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

精彩评论

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