I have a test where I'm trying to observe the behaviour of the Navigator
when the app navigates from page1.dart
to page2.dart
(push) and back (pop). Using verify from the Mockito package, I can successfully verify that the push behaviour works when I am not using pushNamed()
. However, after I changed my project to use Named Route, I wasn't able to find a correct way to implement the mock and test the navigation.
Is there a proper way how to check, that Navigator was called? Or is there a recommended way to mock and replace the navigator
?
Edit: I found a flutter library Mockingjay to Mock the Navigator.
I've found a flutter library Mockingjay to Mock the Navigator.
Here is an example on their GitHub page on testing Navigator.
void main() {
testWidgets('pushes SettingsPage when TextButton is tapped', (tester) async {
final navigator = MockNavigator();
when(() => navigator.push(any())).thenAnswer((_) async {});
await tester.pumpWidget(
MaterialApp(
home: MockNavigatorProvider(
navigator: navigator,
child: const MyHomePage(),
),
),
);
await tester.tap(find.byType(TextButton));
verify(
() => navigator.push(any(that: isRoute<void>(whereName: equals('/settings')))),
).called(1);
});
}