Heikkisorsa
Heikkisorsa

Reputation: 905

When expecting an exception we cannot verify called mocks

I have a unit test in flutter like this:

 test(
        'when an invalid access token is used the api service should throw an exception',
        () async {
          final authService = getAndRegisterAuthService(invalidToken: true);
          final service = APIServiceNetworkImpl();

          const request = APIRequest("/test");
          expect(
            () async => await service.get(request),
            throwsA(isA<DioException>()),
          );
          // CRITICAL LINE
          verify(authService.getAccessToken()).called(1);
        },
      );

Everything works as expected if I comment out the last line. But when I want to verify that a mocked method (in this case getAccessToken) is called, the test fails, but it does not show an error. How can I verify a mocked function call has happened in such a case?

Also, the verify works in a test case where the service does not throw an exception.

Upvotes: 0

Views: 41

Answers (0)

Related Questions