NavigationIntermediate30 XP3 min read
How do you test navigation in Flutter?
TL;DR: Use a mock NavigatorObserver in widget tests to verify navigation calls. For go_router, use GoRouter with a MemoryNavigationAdapter in tests, or check navigation via find.byType after pumping.
Full Answer
Navigation testing verifies that tapping a button or completing an action causes the expected navigation. Two approaches: mock observer pattern and find-widget pattern.
- ▸MockNavigatorObserver with mockito — intercepts push/pop calls
- ▸NavigatorState via tester.state(find.byType(Navigator)) — check route
- ▸go_router: wrap with MaterialApp.router; check screen by finding widgets
- ▸Integration tests: fully exercise navigation on a device
🎯
For go_router tests, the simplest approach is to check that the expected screen widget appears after tapping — no mock needed, just use find.byType(TargetScreen).
Code Examples
dartTesting go_router navigation
Output
Test passes when ProductDetailScreen appears after tapping 'Product 1'
Common Mistakes
- ✗Not calling pumpAndSettle() after navigation — animation isn't complete, target screen not found
- ✗Testing go_router without MaterialApp.router wrapper — GoRouter requires RouterDelegate setup
Interview Tip
💡
Show both the router-level test and the widget-level find pattern — this covers simple and complex navigation test scenarios.
#navigation-testing#widget-test#NavigatorObserver#go_router-testing