0

I have this piece of code mocking a response:

(isFirstSubmit is initialized to true)

     when(
      mockProfileRepository.submitProfile(
        localNumber: localNumber,
        profileContactInfo: fakeProfileContactInfo.copyWith(
          email: modifiedEmail,
          phone: modifiedPhone,
        ),
      ),
    ).thenAnswer((_) async {
      if (isFirstSubmit) {
        debugPrint('Saving profile first time');
        isFirstSubmit = false;
        throw OrderInProgressException();
      } else {
        debugPrint('Saving profile...');
        return Future.value(true);
      }
    });

However the throw OrderInProgressException makes the test abort instead of being part of the equation...

So what I want (just to be clear) is to mock returning an OrderInProgressException (which the submitProfile method would) first time and a Future.value(true) the second time.

How can I mock a condition like this?

Ove Stoerholt
  • 3,843
  • 4
  • 25
  • 33
  • It's unclear what you intend to do. Why are you throwing the exception? What is supposed to catch it? – jamesdlin Jan 12 '22 at 11:39
  • @jamesdlin Better now? – Ove Stoerholt Jan 12 '22 at 12:06
  • Why do you think there's a problem with the mock? If your mock is mimicking the behavior of the real function by throwing an exception, then if that causes your test to fail, that's a problem with your test, not with your mock. Your test needs to expect the exception. Perhaps see [Testing exceptions in Dart](https://stackoverflow.com/q/66324460/). – jamesdlin Jan 12 '22 at 14:16

0 Answers0