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

Navigator.pop is never called while it actually was #67

Open
egonm12 opened this issue Feb 6, 2024 · 0 comments
Open

Navigator.pop is never called while it actually was #67

egonm12 opened this issue Feb 6, 2024 · 0 comments

Comments

@egonm12
Copy link

egonm12 commented Feb 6, 2024

Describe the bug
When I open a dialog with showDialog and I pop it, verify tells me it never called .pop while it did.

popping...
popped!
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure was thrown running a test:
No matching calls. All calls: [VERIFIED] MockNavigator.canPop()
(If you called `verify(...).called(0);`, please instead use `verifyNever(...);`.)

When the exception was thrown, this was the stack:
#0      fail (package:matcher/src/expect/expect.dart:149:31)
#1      _VerifyCall._checkWith (package:mocktail/src/mocktail.dart:728:7)
#2      _makeVerify.<anonymous closure> (package:mocktail/src/mocktail.dart:519:18)
#3      main.<anonymous closure>.<anonymous closure> (file:///Users/path/to/file/flutter_mockingjay/test/main_test.dart:33:13)
<asynchronous suspension>
#4      testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:168:15)
<asynchronous suspension>
#5      TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:1013:5)
<asynchronous suspension>
<asynchronous suspension>
(elided one frame from package:stack_trace)

The test description was:
  can pop a dialog
════════════════════════════════════════════════════════════════════════════════════════════════════
Test failed. See exception logs above.
The test description was: can pop a dialog

✖ MainApp can pop a dialog
popping...
popped!
✓ MainApp can pop a normal widget

To Reproduce

Reproducible example: https://github.com/egonm12/flutter_mockingjay/tree/main

void main() {
  group(MainApp, () {
    late MockNavigator mockNavigator;

    setUp(() {
      mockNavigator = MockNavigator();

      when(() => mockNavigator.canPop()).thenReturn(true);
      when(() => mockNavigator.pop(any())).thenReturn(null);
    });

    testWidgets('can pop a dialog', (WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
          home: MockNavigatorProvider(
            navigator: mockNavigator,
            child: const MyFloatingActionButton(),
          ),
        ),
      );
      await tester.tap(find.byType(FloatingActionButton));
      await tester.pump();
      await tester.pumpAndSettle();

      await tester.tap(find.byType(TextButton));

      verify(() => mockNavigator.canPop()).called(1);
      verify(() => mockNavigator.pop<Object?>(any())).called(1);
    });

    testWidgets('can pop a normal widget', (WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
          home: MockNavigatorProvider(
            navigator: mockNavigator,
            child: const MyWidget(),
          ),
        ),
      );
      await tester.tap(find.byType(ElevatedButton));

      verify(() => mockNavigator.canPop()).called(1);
      verify(() => mockNavigator.pop<Object?>(any())).called(1);
    });
  });
}

Expected behavior

All tests pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Needs Triage
Development

No branches or pull requests

1 participant