Search code examples
fluttertestingmockitohttprequest

MockClient 1 positional argument(s) expected, but 0 found


I'm trying to create some tests for flutter app with Mockito.

This is my code:

@GenerateMocks([http.Client])
main() {
  group('sendUrlPost', () {
    test('returns alias and links if the post call is successful', () async {
      final client = MockClient();
      String apiUrl = "someApiURL";
      String longUrl = "someUrl";
      when(client.post(Uri.parse(apiUrl),
          headers: {'Content-Type': 'application/json'},
          body: jsonEncode(
              {"url": longUrl}))).thenAnswer((_) async => http.Response(
          '{"alias":"70492","_links":{"self":"https://www.youtube.com","short":"https://url-shortener-nu.herokuapp.com/short/70492"}}',
          200));
      expect(await UrlPostConnector.sendUrlPost(longUrl), isA<UrlResponse>());
    });
  });
}

I have followed exactly what the official guide has: https://docs.flutter.dev/cookbook/testing/unit/mocking but when I create the mock client I get the following error:

final client = MockClient();

1 positional argument(s) expected, but 0 found. Try adding the missing arguments

enter image description here

I'm guessing the guide is outdated but I haven't found a way to make it work. I would apprecieate if you can tellme what I'm doing wrong and if there is a better way to test an Http request.

Thanks


Solution

  • I was missing the generated mocks.dart file that is created by running

     flutter pub run build_runner build
    

    Thanks to @jamesdlin for pointing that out.