Return different objects from FakeItEasy A.CallTo()
Asked Answered
W

2

9

For my test, I need the first call to a stub to return one object, and the next call to return a different object. I have seen this in other mock object frameworks in record() blocks, but I have not figured out how to do it in FakeItEasy. FakeItEasy is the mandated framework in our shop, and I am using AutoFixture to generate fakes.

I looked at NextCall, but it doesn't look like I can specify a return value.

Here is an idea of what I'd like to do:

ReceiveMessageResponse queueResponse1 = fixture.Create<ReceiveMessageResponse>();
ReceiveMessageResponse queueResponse2 = fixture.Create<ReceiveMessageResponse>(seed);
A.CallTo(() => sqsClient.ReceiveMessage(null)).WithAnyArguments().Returns(queueResponse1);
//The following should happen the second time...
A.CallTo(() => sqsClient.ReceiveMessage(null)).WithAnyArguments().Returns(queueResponse2);

Any help is appreciated.

Wellmannered answered 6/3, 2013 at 16:51 Comment(0)
P
18

Two ways to do it, one of them is the one you refer to in your own answer:

A.CallTo(() => foo.Bar()).ReturnsNextFromSequence(new[] { response1, response2 });

The other way is:

A.CallTo(() => foo.Bar()).Returns(response2);
A.CallTo(() => foo.Bar()).Returns(response1).Once();
Propitious answered 7/3, 2013 at 19:12 Comment(1)
Order in second approach is important for getting it workCondole
W
7

And of course, it happens every time. Within several minutes of posting I come across the answer on my own.

I ended up using:

ReturnsNextFromSequence(new [] {queueResponse1, queueResponse2});

I'm not sure it's the preferred method, but it works great for me.

Wellmannered answered 6/3, 2013 at 17:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.