テスト

ナビゲーションのウィジェットテストは、flutter run と同じく SpringApplicationWidget でアプリを起動し、RouteTemplate Bean を通じてナビゲートします。ルート、シェル、フィルター、ガードは本物です。

使う理由

ルートは起動時にチェックされるため、ナビゲーションの最初のテストは単にアプリが起動することです。その後、テストはユーザーのように (タップ)、またはリンクのように (go('/address')) ルーターを操作し、どの画面が表示されるかをチェックします。

例

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);
  });
}

関連項目