Return various objects from FakeItEasy A.CallTo ()

For my test, I need the first call to the stub to return one object, and the next call to return another object. I saw this in other object mockups in record () blocks, but I did not understand how to do this in FakeItEasy. FakeItEasy is the credential structure in our store, and I use AutoFixture to create fakes.

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

Here is an idea of ​​what I would 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.

+8
unit-testing fakeiteasy
source share
2 answers

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

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

Another way:

 A.CallTo(() => foo.Bar()).Returns(response2); A.CallTo(() => foo.Bar()).Returns(response1).Once(); 
+14
source share

And of course, this happens every time. Within a few minutes after publication, I myself meet with the answer.

I ended up using:

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

I'm not sure if this is the preferred method, but it works fine for me.

+7
source share

All Articles