搜尋套件、儲存庫與 APP 分類

套件詳情v0.8.2

mock_exceptions

提供了一種在特定呼叫時拋出例外狀況的機制。這在使用 Fake 時非常有用,我們仍希望偶爾讓其拋出例外狀況。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
35.3萬
喜歡數
5
Pub 分數
150

安裝套件

flutter pub add mock_exceptions

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

mock_exceptions 套件讓開發者能在測試期間模擬 mock 物件中的例外行為。它允許對虛擬或 mock 物件的特定方法呼叫拋出例外,這在不需要依賴實際實作的情況下測試錯誤處理情境時特別有用。透過驗證程式碼如何回應失敗,可確保測試案例的穩健性。此套件與 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 項

原始碼儲存庫

使用此套件的專案