パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v0.8.2

mock_exceptions

特定の呼び出しで例外をスローするメカニズムを提供します。これは、Fake を使用している場合に役立ちます。例外を意図的に発生させたい場合に便利です。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
38.1万
いいね数
5
Pub ポイント
150

パッケージをインストール

flutter pub add mock_exceptions

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

mock_exceptions パッケージは、テスト中にモックオブジェクトで例外の挙動をシミュレートできるようにします。特定のメソッド呼び出しを偽物またはモックに対して例外をスローするように設定でき、これは実装に依存せずにエラー処理のシナリオをテストするのに特に役立ちます。コードが障害にどう対応するかを検証することで、テストケースの堅牢性を確保できます。このパッケージはFlutterのテストエコシステムとシームレスに統合されており、すべての主要プラットフォームをサポートしています。制御された障害状態が必要なユニットテストや統合テストに最適です。

用途

  • エラー処理パスのテスト
  • 失敗したAPI応答のシミュレーション
  • 再試行ロジックの検証
  • グレースフルデグレードの確保
  • 例外回復のテスト
  • 一貫しない動作のモック

主な機能

  • 簡単な例外の挿入
  • すべての Flutter プラットフォームをサポートしています
  • Fake クラスとの統合
  • 最小限のセットアップが必要
  • 柔軟な例外設定
  • 既存のテストフレームワークと連携可能

適している対象

  • ユニットテスト担当者
  • 統合テスト担当者
  • レジリエントなコードを書く開発者
  • TDD/BDD を使用するチーム
  • 現実的な失敗シナリオを必要とするユーザー
  • テスト駆動開発ワークフロー

注意事項

  • モックの概念を理解している必要がある
  • 本番環境での使用を想定していません
  • 適切なテスト構造に依存する
  • 過度に使用すると、テストの複雑さが増す可能性があります
  • 他のテストユーティリティと併用するのが最適です
  • 例外のシミュレーションのみに限定

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

mock_exceptions

pub package Dart

Provides a mechanism to throw exceptions on certain calls. This is useful when working with a Fake and we still want to occasionally make it throw exceptions. At a glance:

final f = MyFake();
whenCalling(Invocation.method(#doSomething, null))
    .on(f)
    .thenThrow(Exception());
expect(() => f.doSomething(), throwsException);

Features

  • supports mocking exceptions for methods, getters, setters.
  • supports matching on positional parameters, named parameters and type parameters (generics).
  • supports regular matchers.
  • supports omitting parameters when matching with anything.

For exhaustive usage, see our unit tests.

Differences with Mockito

Mockito lets you mock and stub methods. That means it lets you return predefined responses and throw exceptions, but not act as closely to the real thing as a Fake. Since mock_exceptions is supposed to be used on Fakes, its API unambiguously lets you mock only exceptions.

Usage

  1. In your Fake method/getter/setter implementation, add maybeThrowException at the beginning.
  2. In your unit test, set up an expectation, then verify your expectations as usual.
Before

Your Fake method might look like this. It does some real work.

class MyFake {
  String doSomething(String input) {
    return 'it works';
  }
}

Your unit test might even check that it works.

final fake = MyFake();
expect(fake.doSomething('yes'), 'it works');
After

Your Fake method checks for possible exceptions before doing the work.

class MyFake {
  String doSomething(String input) {
    maybeThrowException(this, Invocation.method(#doSomething, [input]));
    return 'it works';
  }
}

You can now forcefully throw exceptions and test for them.

final fake = MyFake();
whenCalling(Invocation.method(#doSomething, ['fun']))
    .on(fake)
    .thenThrow(Exception());
expect(() => fake.doSomething('fun'), throwsException);

Design considerations

  • whenCalling(Invocation.method(#doSomething, [equals('fun')])).on(fake).thenThrow(Exception()); is too verbose. Why not reimplement Mockito's API so that I can write when(fake.doSomething('fun')).thenThrow(Exception())?
    • Implementing Mockito's API requires relying on the noSuchMethod trick to detect the Invocation. As of writing, Mockito's mock.dart takes 1200 lines of code while our mock_exceptions.dart takes around 80. Even if we pare Mockito's down to the minimum (excluding verifications, captures), it'd still take around 500 lines of code.
    • It also forces the Fakes to extend a pre-defined class, Mock in Mockito's case. In some projects such as Fake Cloud Firestore (example), we actually need to extend another class.
  • The Mockito API has its own downsides and gotchas. See how one should use named arguments here. Also you need to use its wrappers around the regular Matchers, such as any instead of anything, and argThat(matcher).

カテゴリー

依存関係

依存関係一覧 1 件

ソースリポジトリ

このパッケージを使用しているプロジェクト