
# テスト

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

## 使う理由

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

## 例

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

- `RouteTemplate` Bean は一番上の画面を読み、操作します。`state`、`uri`、`stack` がアプリの現在地を示します。
- フェイクやテスト用のプロパティが必要なテストは、`springApplication` のコピーにオーバーライドを加えて組み立てた独自の `SpringApplication` を渡します (`SpringApplicationWidget` 自体はオーバーライドを受け取りません)。
- ウィジェットテストではプラットフォームは Android なので、プラットフォームのディープリンクは関与しません。`go` がリンクの役割を果たします。

## 関連項目

- [spring_boot_starter_flutter のテスト](../spring-boot-starter-flutter/testing.md)
- [画面とルート](screens.md)
