type 'MockResponse' is not a subtype of type 'Map<String, dynamic>' in type cast

Viewed 34

I have been following documentation in mocktail and in the flutter bloc documentation to learn how to write tests. In the weather example project, they make http request directly in the weather api package. In my project (basic project using google books api), I am splitting that http request logic to another package which I then call in the api package. I know the code itself works because I've manually tested it but I can't get the automated tests to pass.

Here is a snippet of it.

class MockBaseApi extends Mock implements BaseApi {}

class MockResponse extends Mock implements http.Response {}

class FakeUri extends Fake implements Uri {}

void main() {
  late BaseApi baseApi;
  late GoogleBooksApiClient googleBooksApiClient;

  setUpAll(() {
    registerFallbackValue(FakeUri());
  });

  setUp(() {
    baseApi = MockBaseApi();
    googleBooksApiClient = GoogleBooksApiClient(baseApi: baseApi);
  });

  group('getBooks', () {
    test('returns books on valid response', () async {
      final mockResponse = MockResponse();

      when(() => mockResponse.statusCode).thenReturn(200);
      when(() => mockResponse.body).thenReturn(//some mock response)
      when(() => baseApi.get(
              'www.googleapis.com', '/books/v1/volumes', {'q': 'harrypotter'}))
          .thenAnswer(
        (_) async => mockResponse,
      );
      final response = await googleBooksApiClient.getBooks('harrypotter');
    }
  }
}

The error I get when running the test is as written in the title: type 'MockResponse' is not a subtype of type 'Map<String, dynamic>' in type cast. I'm honestly completely clueless on how to even figure out what the issue is here and how to fix it.

Here is the repo: https://github.com/Dan-Y-Ko/Flutter-Dart-Playground/tree/master/flutter/projects/chingu_bookfinder_flutter/packages

0 Answers
Related