Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FakeItEasy configure fake to throw exception and return value on the next call

We have to implement a retry-mechanism.

To test the RetryProvider, I want a fake of a class to throw exceptions on the first two calls, but return a valid object on the third call.

Under normal circumstances (without throwing exceptions) we could use A.CallTo(() => this.fakeRepo.Get(1)).ReturnsNextFromSequence("a", "b", "c");

I want something similar:

  • First Call: throw new Exception();
  • Second Call: throw new Exception();
  • Third Call: return "success";

How can I configure my fake to do this?

Thanks in advance

like image 991
xeraphim Avatar asked Dec 13 '22 20:12

xeraphim


1 Answers

var fakeRepo = A.Fake<IFakeRepo>();

A.CallTo(() => fakeRepo.Get(1))
     .Throws<NullReferenceException>()
     .Once()
     .Then
     .Throws<NullReferenceException>()
     .Once()
     .Then
     .Returns('a');

See more about this at Specifying different behaviors for successive calls.

like image 194
TheRock Avatar answered May 04 '23 01:05

TheRock