LoginSignup
5
1

More than 1 year has passed since last update.

[Flutter] テスト実行時にMissingPluginExceptionが発生する

Last updated at Posted at 2022-02-24

Overview

Flutterのテスト実行時に以下のようなエラーが出現するようになった。

> flutter test
...
00:10 +0 -9: /path/to/test.dart: hogehoge scenario [E]
  MissingPluginException(No implementation found for method getApplicationDocumentsDirectory on channel plugins.flutter.io/path_provider_macos)
  package:flutter/src/services/platform_channel.dart 175:7  MethodChannel._invokeMethod
...

環境

❯ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.10.2, on macOS 11.5.2 20G95 darwin-arm, locale ja-JP)
[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.1)
[✓] IntelliJ IDEA Community Edition (version 2021.2.1)
[✓] VS Code (version 1.63.2)
[✓] Connected device (1 available)
[✓] HTTP Host Availability

• No issues found!

原因

Dartのunit test実行時はpluginをそのまま使うことはできないらしい。

(参考)
https://github.com/flutter/flutter/issues/95970

解決法

使おうとしているpluginのMockを定義すれば解決する模様。

今回はテスト実行時にgetApplicationDocumentsDirectoryでパスを取得しようとして失敗しているっぽいので、以下のようにsetUpAll()でMockを定義するように変更したところ、エラーが発生しなくなった。

  setUpAll(() {
    TestWidgetsFlutterBinding.ensureInitialized();
    const MethodChannel channel = MethodChannel('plugins.flutter.io/path_provider_macos');
    channel.setMockMethodCallHandler((methodCall) async {
      return ".";
    });
  });
5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1