Search code examples
c#.netunit-testingmockingrhino-mocks

Return different instances for each call using rhino mocks


I've got this code:

Expect.Call(factory.CreateOrder())
    .Return(new Order())
    .Repeat.Times(4);

When this is called four times, every time the same instance is returned. I want difference instances to be returned. I would like to be able to do something like:

Expect.Call(factory.CreateOrder())
    .Return(() => new Order())
    .Repeat.Times(4);

Can this be done in some way?


Solution

  • Instead of using

    .Return(new Order());
    

    Try using

    .Do((Func<Order>)delegate() { return new Order(); });
    

    This will call the delegate each time, creating a new object.