expectRedirect function

Future<void> expectRedirect(
  1. WidgetTester tester,
  2. GoRouter router, {
  3. required String from,
  4. required String to,
})

Opens from and checks that the router ends up at to.

Use it to test guards and redirects:

testWidgets('signed-out users go to login', (tester) async {
  await pumpRouter(tester, router);
  await expectRedirect(tester, router, from: '/settings', to: '/login');
});

Implementation

Future<void> expectRedirect(
  WidgetTester tester,
  GoRouter router, {
  required String from,
  required String to,
}) async {
  router.go(from);
  await tester.pumpAndSettle();
  expect(router, isAt(to), reason: 'Expected $from to redirect to $to');
}