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

mocking image is affecting other image mocks #250

Open
ryzizub opened this issue Oct 17, 2024 · 0 comments
Open

mocking image is affecting other image mocks #250

ryzizub opened this issue Oct 17, 2024 · 0 comments

Comments

@ryzizub
Copy link

ryzizub commented Oct 17, 2024

Describe the bug
I'm using mockNetworkImages and also in same group of tests im doing two tests that are cowering errorBuilder, so im mocking trough HttpOverride failing HttpClient. Seems that if I run those tests separately it's running ok, but if I run them as a group those which are running later are failing cause either there is mocked failing client or successful client.

To Reproduce
I included minimum reproducible example

import 'dart:io';

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

import 'helpers/pump_app.dart';

class _MockHttpClient extends Mock implements HttpClient {}

void main() {
  group('testing image', () {
    testWidgets('successful render', (tester) async {
      await mockNetworkImages(() {
        return tester.pumpApp(
          const _RandomImageWidget(
            src: 'https://testing.com/image.png',
          ),
        );
      });

      expect(find.byType(Image), findsOneWidget);
      expect(find.byType(_ErrorContainer), findsNothing);
    });

    testWidgets('not sucessful render', (tester) async {
      final httpClient = _MockHttpClient();

      when(
        () => httpClient.getUrl(
          Uri.parse(
            'https://testing.com/image2.png',
          ),
        ),
      ).thenThrow(Exception());

      await HttpOverrides.runZoned(
        () async {
          await tester.pumpApp(
            const _RandomImageWidget(
              src: 'https://testing.com/image2.png',
            ),
          );
          await tester.pump();
        },
        createHttpClient: (_) => httpClient,
      );

      expect(find.byType(Image), findsOneWidget);
      expect(find.byType(_ErrorContainer), findsOneWidget);
    });
  });
}

class _RandomImageWidget extends StatelessWidget {
  const _RandomImageWidget({
    required this.src,
  });

  final String src;

  @override
  Widget build(BuildContext context) {
    return Image.network(
      src,
      errorBuilder: (context, error, _) {
        return _ErrorContainer();
      },
    );
  }
}

class _ErrorContainer extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

Expected behavior
I should be able to mock both behaviour in the same group

Screenshots
Snímek obrazovky 2024-10-17 v 20 55 54
Snímek obrazovky 2024-10-17 v 20 56 11

**Logs **
Run flutter analyze and attach any output of that command below.
If there are any analysis errors, try resolving them before filing this issue.

No issues found! (ran in 0.9s)

Paste the output of running flutter doctor -v here.

[✓] Flutter (Channel stable, 3.24.3, on macOS 15.0.1 24A348 darwin-arm64, locale cs-CZ)
    • Flutter version 3.24.3 on channel stable at /Users/ryzizub/.asdf/installs/flutter/3.24.3-stable
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 2663184aa7 (5 weeks ago), 2024-09-11 16:27:48 -0500
    • Engine revision 36335019a8
    • Dart version 3.5.3
    • DevTools version 2.37.3

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/ryzizub/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.0)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 16A242d
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2024.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314)

[✓] VS Code (version 1.94.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.98.0

Additional context
None

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