【 Flutter 】Flutter を 基礎 から 学習 ( アーキテクチャ ) part266 Widgetのテスト

基礎 から 学ぶ Flutter 」という書籍で  学習 したことを ブログでアウトプットしていこうと思います。今回は テスト ( part266 )です。

前回

【 Flutter 】Flutter を 基礎 から 学習 ( テスト ) part265 単体テスト

引き続き、テストについて学びます。

Widgetのテスト

WidgetのテストはAPIやデータベースを呼び出すクラスをもモックにするのが良いとのこと。

mockitoを使用しないで簡単なスタブクラスを作ればらしいです。

mockitoを使ったテストではNullセーフティー関連のエラーでなにも成功していなかったので朗報です!

以下のような結果になりました。

いや・・・分かっていました。分かっていましたとも😂

D:\flutter\bin\flutter.bat --no-color test --machine --start-paused test\widget_bloc_test.dart
Testing started at 21:11 ...

called _WidgetA#build()
called _WidgetB#build()
called _WidgetC#build()
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure was thrown running a test:
Expected: no matching nodes in the widget tree
  Actual: _TextFinder:
   Which: means one was found but none were expected

When the exception was thrown, this was the stack:
#4      main. (file:///D:/flutter_projects/flutter_app/test/widget_bloc_test.dart:39:5)


(elided one frame from package:stack_trace)

This was caught by the test expectation on the following line:
  file:///D:/flutter_projects/flutter_app/test/widget_bloc_test.dart line 39
The test description was:
  BLoCのカウントアップの画面のテスト
════════════════════════════════════════════════════════════════════════════════════════════════════

Test failed. See exception logs above.
The test description was: BLoCのカウントアップの画面のテスト

最後に

これもまた失敗。

expect(find.text('10'), findsNothing);の検証で失敗ていたようなのでmockitoの時とは状況が異なるようでした。

今日はここまで!

参考