سؤال

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.

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top