felangel / mocktail

A mock library for Dart inspired by mockito
https://pub.dev/packages/mocktail
MIT License
588 stars 80 forks source link

Incompatibility with new versions of `flutter_svg` #239

Closed pedrox-hs closed 3 weeks ago

pedrox-hs commented 1 month ago

Describe the bug

In recent versions of the flutter_svg package, using the mockNetworkImages method results in the following error:

The following _TypeError was thrown running a test (but after the test had completed):
type 'Null' is not a subtype of type 'Future<HttpClientRequest>'

This issue occurs when attempting to mock network images using mockNetworkImages in widget tests that include SVG images.

To Reproduce Steps to reproduce the behavior:

  1. Add flutter_svg package;
  2. Create a widget that displays an SVG image from network using SvgPicture.network;
  3. Use the mockNetworkImages utility method to mock the network image in a widget test;
  4. Run the test.

Example Code

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail_image_network/mocktail_image_network.dart';

class FakeApp extends StatelessWidget {
  const FakeApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: SvgPicture.network(
            // URL to the Flutter logo from https://flutter.dev/brand
            'https://storage.googleapis.com/cms-storage-bucket/847ae81f5430402216fd.svg',
          ),
        ),
      ),
    );
  }
}

void main() {
  testWidgets('can use mocktail for network images', (tester) async {
    await mockNetworkImages(() async => tester.pumpWidget(const FakeApp()));
    expect(find.byType(SvgPicture), findsOneWidget);
  });
}

Expected behavior

The widget test should successfully mock the SVG image without any type errors, allowing for proper rendering and testing of the widget.

Logs

00:02 +0: can use mocktail for network images                                                                                                                                                          
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following _TypeError was thrown running a test:
type 'Null' is not a subtype of type 'Future<HttpClientRequest>'

When the exception was thrown, this was the stack:
#0      _MockHttpClient.openUrl (package:mocktail_image_network/src/mocktail_image_network.dart:52:7)
#1      IOClient.send (package:http/src/io_client.dart:88:38)
#2      BaseClient._sendUnstreamed (package:http/src/base_client.dart:93:38)
...
#882    WidgetTester.pumpWidget (package:flutter_test/src/widget_tester.dart:591:27)
#883    main.<anonymous closure>.<anonymous closure> (file:///.../mocktail/packages/mocktail_image_network/example/test/image_test.dart:8:48)
#888    HttpOverrides.runZoned (dart:_http/overrides.dart:55:23)
#889    mockNetworkImages (package:mocktail_image_network/src/mocktail_image_network.dart:44:24)
#890    main.<anonymous closure> (file:///.../mocktail/packages/mocktail_image_network/example/test/image_test.dart:8:11)
#891    testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:183:29)
<asynchronous suspension>
#892    TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:1017:5)
<asynchronous suspension>
<asynchronous suspension>
(elided 9 frames from dart:async and package:stack_trace)

The test description was:
  can use mocktail for network images
════════════════════════════════════════════════════════════════════════════════════════════════════

00:02 +0 -1: can use mocktail for network images [E]                                                                                                                                                   
  Test failed. See exception logs above.
  The test description was: can use mocktail for network images

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following _TypeError was thrown running a test (but after the test had completed):
type 'Null' is not a subtype of type 'Future<HttpClientRequest>'

When the exception was thrown, this was the stack:
#0      _MockHttpClient.openUrl (package:mocktail_image_network/src/mocktail_image_network.dart:52:7)
#1      IOClient.send (package:http/src/io_client.dart:88:38)
#2      BaseClient._sendUnstreamed (package:http/src/base_client.dart:93:38)
...
#882    WidgetTester.pumpWidget (package:flutter_test/src/widget_tester.dart:591:27)
#883    main.<anonymous closure>.<anonymous closure> (file:///.../mocktail/packages/mocktail_image_network/example/test/image_test.dart:8:48)
#888    HttpOverrides.runZoned (dart:_http/overrides.dart:55:23)
#889    mockNetworkImages (package:mocktail_image_network/src/mocktail_image_network.dart:44:24)
#890    main.<anonymous closure> (file:///.../mocktail/packages/mocktail_image_network/example/test/image_test.dart:8:11)
#891    testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:183:29)
<asynchronous suspension>
#892    TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:1017:5)
<asynchronous suspension>
<asynchronous suspension>
(elided 9 frames from dart:async and package:stack_trace)
════════════════════════════════════════════════════════════════════════════════════════════════════
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following _TypeError was thrown running a test (but after the test had completed):
type 'Null' is not a subtype of type 'Future<HttpClientRequest>'

When the exception was thrown, this was the stack:
#0      _MockHttpClient.openUrl (package:mocktail_image_network/src/mocktail_image_network.dart:52:7)
#1      IOClient.send (package:http/src/io_client.dart:88:38)
#2      BaseClient._sendUnstreamed (package:http/src/base_client.dart:93:38)
...
#882    WidgetTester.pumpWidget (package:flutter_test/src/widget_tester.dart:591:27)
#883    main.<anonymous closure>.<anonymous closure> (file:///.../mocktail/packages/mocktail_image_network/example/test/image_test.dart:8:48)
#888    HttpOverrides.runZoned (dart:_http/overrides.dart:55:23)
#889    mockNetworkImages (package:mocktail_image_network/src/mocktail_image_network.dart:44:24)
#890    main.<anonymous closure> (file:///.../mocktail/packages/mocktail_image_network/example/test/image_test.dart:8:11)
#891    testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:183:29)
<asynchronous suspension>
#892    TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:1017:5)
<asynchronous suspension>
<asynchronous suspension>
(elided 9 frames from dart:async and package:stack_trace)
════════════════════════════════════════════════════════════════════════════════════════════════════

To run this test again: dart test test/image_test.dart -p vm --plain-name 'can use mocktail for network images'

00:02 +0 -1: Some tests failed.                                                                                                                                                                        

Additional context

Flutter SVG version: 2.0.10+1

Flutter version:

Flutter 3.19.6 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 54e66469a9 (5 weeks ago) • 2024-04-17 13:08:03 -0700
Engine • revision c4cd48e186
Tools • Dart 3.3.4 • DevTools 2.31.1