How to mock http request in flutter integration test? How to mock http request in flutter integration test? dart dart

How to mock http request in flutter integration test?


The solution I found was to define the mock in test_driver/app.dart and call the runApp function after that:

import 'package:flutter/widgets.dart';import 'package:flutter_driver/driver_extension.dart';import 'package:shared_preferences/shared_preferences.dart';import 'package:utgard/business/config/globals.dart';import 'package:utgard/main.dart' as app;class MockClient extends Mock implements http.Client {}void main() {  enableFlutterDriverExtension();  final MockClient client = MockClient();  // make your mocks here  httpClient = client;  runApp(app.MyApp());}


Instead of

      when(client.post('http://wwww.google.com'))          .thenAnswer((_) async => http.Response('{"title": "Test"}', 200));

try any and then assert it later

        when(          mockHttpClient.send(any),        ).thenAnswer((_) async => http.Response('{"title": "Test"}', 200));// ...        final String capt = verify(client.send(captureAny)).captured;        expect(capt, 'http://wwww.google.com');

There's a small chance the call param is not exactly what you mock, so go with any is safer.