Testing

A widget test of the navigation starts the app as flutter run does, with SpringApplicationWidget, and navigates through the RouteTemplate bean. The routes, shells, filters and guards are the real ones.

Why

Routes are checked at the start, so the first test of the navigation is simply that the app starts. After that, a test drives the router as the user would (a tap) or as a link would (go('/address')), and checks which screen shows.

Example

import 'package:flutter_test/flutter_test.dart';
import 'package:my_app/main.dart';
import 'package:my_app/spring_gen/main.spring.dart';
import 'package:spring_boot_starter_flutter_navigation/spring_boot_starter_flutter_navigation.dart';

void main() {
  testWidgets('an unknown address shows the not-found screen', (tester) async {
    await tester.pumpWidget(
      SpringApplicationWidget(application: () => springApplication),
    );
    await tester.pumpAndSettle();

    final context = SpringScope.of(tester.element(find.byType(CatalogPage)));
    await context.getBean<RouteTemplate>().go('/nope');
    await tester.pumpAndSettle();

    expect(find.byType(NotFoundPage), findsOneWidget);
  });

  testWidgets('a book opens over the catalog, and back returns', (tester) async {
    await tester.pumpWidget(
      SpringApplicationWidget(application: () => springApplication),
    );
    await tester.pumpAndSettle();

    final routes = SpringScope.of(tester.element(find.byType(CatalogPage)))
        .getBean<RouteTemplate>();
    await routes.go(BookPageRoute.to(isbn: '978-0441013593'));
    await tester.pumpAndSettle();
    expect(find.byType(BookPage), findsOneWidget);

    routes.pop();
    await tester.pumpAndSettle();
    expect(find.byType(CatalogPage), findsOneWidget);
  });
}

See also