Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return different objects from FakeItEasy A.CallTo()

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.

like image 730
Cork Avatar asked Mar 06 '13 16:03

Cork


1 Answers

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();
like image 58
Patrik Hägne Avatar answered Sep 19 '22 14:09

Patrik Hägne