Sub topic 17
Testing routes and redirects
Assume you know Dart null safety, Flutter widgets, Navigator, GoRouter, and ChangeNotifier. This chapter uses widget tests. A widget test builds part of the app, performs actions, and checks what appears on screen.
Routing tests should verify observable behavior:
- The correct location displays the correct screen.
- A path parameter reaches the destination.
- A redirect sends a user to the expected location.
- A state change causes a redirect to run again.
- Navigation does not create an invalid screen stack.
Build a testable router
Keep router creation in a function instead of hiding application state in global variables. The test can then create a fresh authentication state and router for every test.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:go_router/go_router.dart';
class AuthState extends ChangeNotifier {
bool loggedIn = false;
void logIn() {
loggedIn = true;
notifyListeners();
}
}
GoRouter createRouter(AuthState auth) {
return GoRouter(
initialLocation: '/',
refreshListenable: auth,
redirect: (context, state) {
final atLogin = state.matchedLocation == '/login';
if (!auth.loggedIn) {
return atLogin ? null : '/login';
}
return atLogin ? '/' : null;
},
routes: [
GoRoute(
path: '/',
builder: (context, state) => Scaffold(
body: ElevatedButton(
onPressed: () => context.go('/books/42'),
child: const Text('Open book'),
),
),
),
GoRoute(
path: '/login',
builder: (context, state) => Scaffold(
body: ElevatedButton(
onPressed: auth.logIn,
child: const Text('Log in'),
),
),
),
GoRoute(
path: '/books/:id',
builder: (context, state) => Text(
'Book ${state.pathParameters['id']}',
),
),
],
);
}
void main() {
testWidgets('redirects signed-out users to login', (tester) async {
final auth = AuthState();
final router = createRouter(auth);
await tester.pumpWidget(
MaterialApp.router(routerConfig: router),
);
expect(find.text('Log in'), findsOneWidget);
expect(find.text('Open book'), findsNothing);
});
testWidgets('rechecks the redirect after login', (tester) async {
final auth = AuthState();
final router = createRouter(auth);
await tester.pumpWidget(
MaterialApp.router(routerConfig: router),
);
await tester.tap(find.text('Log in'));
await tester.pumpAndSettle();
expect(find.text('Open book'), findsOneWidget);
expect(find.text('Log in'), findsNothing);
});
testWidgets('navigates to a book path', (tester) async {
final auth = AuthState()..loggedIn = true;
final router = createRouter(auth);
await tester.pumpWidget(
MaterialApp.router(routerConfig: router),
);
await tester.tap(find.text('Open book'));
await tester.pumpAndSettle();
expect(find.text('Book 42'), findsOneWidget);
});
}pumpWidget builds the app. pumpAndSettle lets animations, redirects, and rebuilds finish. Use it after navigation when the route might animate or update asynchronously.
Test locations directly
You can test a deep link without tapping through the UI:
testWidgets('opens a deep link', (tester) async {
final auth = AuthState()..loggedIn = true;
final router = createRouter(auth);
router.go('/books/99');
await tester.pumpWidget(
MaterialApp.router(routerConfig: router),
);
await tester.pumpAndSettle();
expect(find.text('Book 99'), findsOneWidget);
});This test checks that the declared route can handle a URL received from a browser or operating system.
Test the routing flow
Exercise
Why should a redirect test call pumpAndSettle after changing authentication state?
Show answer
The state change notifies the router, which may rebuild and perform a redirect. pumpAndSettle waits for those updates and any route transition to complete before the assertions run.