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

套件詳情v0.0.10

state_notifier_test

一個測試程式庫,可輕鬆測試 StateNotifier。專為與 state_notifier、riverpod 或 flutter_riverpod 套件一起使用而設計。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
1970
喜歡數
22
Pub 分數
150

安裝套件

flutter pub add state_notifier_test

套件介紹

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

一個專為簡化 StateNotifier 類測試而設計的測試程式庫,與 state_notifier、Riverpod 和 flutter_riverpod 相容。它提供實用工具,可輕鬆斷言狀態變更、處理非同步更新,並測試錯誤狀況。此套件可無縫整合至 Flutter 測試工作流程中,讓開發者能為跨多個平台的反應式狀態邏輯撰寫可靠且可維護的測試。

適用情境

  • 測試 StateNotifier 中的狀態轉換
  • 驗證異步狀態更新
  • 驗證狀態邏輯中的錯誤處理
  • 單元測試 Riverpod 為基礎的狀態管理
  • 確保跨平台的一致性行為

主要特色

  • 輕鬆斷言狀態變更
  • 對異步狀態測試的支援
  • 與 Riverpod 生態系統的整合
  • 最小化樣板程式碼
  • 跨平台相容性
  • 測試輔助方法,用於常見情境

適合對象

  • 使用 Riverpod 或 state_notifier 的 Flutter 開發人員
  • 重視測試可靠性的團隊
  • 具有複雜狀態邏輯的專案
  • 需要強大單元測試的 CI/CD 流水線
  • 撰寫乾淨、獨立狀態測試的開發者

使用建議

  • 需要 state_notifier、riverpod 或 flutter_riverpod
  • 僅限基於 StateNotifier 的狀態管理
  • 可能需要為複雜依賴項進行額外設定
  • 不適用於小部件級別的整合測試
  • 最好與其他測試套件如 mockito 或 test 搭配使用

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Package

Package is a port of felangel's bloc_test, modified to work with StateNotifier class

Installation

Add to dev dependencies inside pubspec_yaml:

dev_dependencies:
  state_notifier_test: [version]

Usage

import 'package:state_notifier_test.dart';

stateNotifierTest(
  'CounterNotifier emits [9,10] when seeded with 9',
  build: () => CounterNotifier(),
  seed: () => [9],
  actions: (stateNotifier) => stateNotifier.increment(),
  expect: () => [9,10],
);

class CounterStateNotifier extends StateNotifier<int> {
  CounterStateNotifier() : super(0);

  void increment() {
    state = state + 1;
  }
}

stateNotifierTest can also be used to skip any number of emitted states before asserting against the expected states. skip defaults to 0.

stateNotifierTest(
  'CounterNotifier emits [2] when increment is called twice',
  build: () => CounterNotifier(),
  actions: (stateNotifier) {
    stateNotifier
      ..increment()
      ..increment();
  },
  skip: 1,
  expect: () => [2],
);

stateNotifierTest can also be used to verify internal stateNotifier functionality.

stateNotifierTest(
  'CounterNotifier emits [1] when increment is called',
  build: () => CounterNotifier(),
  actions: (stateNotifier) => stateNotifier.increment(),
  expect: () => [1],
  verify: (_) {
    verify(() => repository.someMethod(any())).called(1);
  }
);

Testing Options

setUp is optional and should be used to set up any dependencies prior to initializing the stateNotifier under test. setUp should be used to set up state necessary for a particular test case. For common set up code, prefer to use setUp from package:test/test.dart.

build should construct and return the stateNotifier under test.

seed is an optional Function that returns an Iterable of States which will be used to seed the stateNotifier before actions is called.

actions is an optional callback which will be invoked with the stateNotifier under test and should be used to interactions with the stateNotifier.

skip is an optional int which can be used to skip any number of states. skip defaults to 0.

expect is an optional Function that returns a Matcher which the stateNotifier under test is expected to emit after actions is executed.

verify is an optional callback which is invoked after expect and can be used for additional verification/assertions. verify is called with the stateNotifier returned by build.

errors is an optional Function that returns a Matcher which the stateNotifier under test is expected to throw after actions is executed.

tearDown is optional and can be used to execute any code after the test has run. tearDown should be used to clean up after a particular test case. For common tear down code, prefer to use tearDown from package:test/test.dart.

expectInitialState is an optional bool.If set to true, the initial state from the constructor will also be checked. Useful when you want to validate the initial state. Note: if set to true, it will only catch the last assigned state in the constructor. expectInitialState defaults to false.

所屬分類

依賴

依賴清單 5 項

原始碼儲存庫

使用此套件的專案