Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using [thenThrow] and closure in [expect] break [verify] method #247

Open
choi88andys opened this issue Jul 18, 2024 · 0 comments
Open

Using [thenThrow] and closure in [expect] break [verify] method #247

choi88andys opened this issue Jul 18, 2024 · 0 comments

Comments

@choi88andys
Copy link

choi88andys commented Jul 18, 2024

Describe the bug
Using thenThrow and closure in expect to test throwing will break verify method.
Only the first mock function - verify(() => http.get(any())).called(1); in this case - is verified.

To Reproduce
Run below test

Code
// Data Source
class RemoteDataSource {
  const RemoteDataSource({
    required Client http,
  }) : _http = http;

  final Client _http;

  Future<void> getSomething() async {
    final uri = Uri.https('example.com', '/path');
    await _http.get(uri);
    await _http.post(uri);
    await _http.put(uri);
  }
}

// Test
class MockHttpClient extends Mock implements Client {}

void main() {
  late MockHttpClient http;
  late RemoteDataSource remoteDataSource;

  setUp(() async {
    registerFallbackValue(Uri());
    http = MockHttpClient();
    remoteDataSource = RemoteDataSource(http: http);
  });

  group('getSomething', () {
    test('post should called once', () {
      when(
        () => http.get(any()),
      ).thenAnswer((invocation) async => Response('', 200));
      when(
        () => http.post(any()),
      ).thenAnswer((invocation) async => Response('', 200));
      when(() => http.put(any())).thenThrow(Exception());

      expect(() => remoteDataSource.getSomething(), throwsA(isA<Exception>()));

      verify(() => http.post(any())).called(1);
    });
  });
}

Expected behavior
verify(() => http.post(any())).called(1); should passed.

Logs

No matching calls. All calls: MockHttpClient.get(https://example.com/path, {headers: null})
(If you called `verify(...).called(0);`, please instead use `verifyNever(...);`.)

Additional context
Dart: 3.4.3
Flutter: 3.22.2
mocktail: 1.0.4
http: 1.2.2

@choi88andys choi88andys changed the title Calling FirebaseMessaging getToken break verify method Using [thenThrow] and closure in [expect] break [verify] method Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant