搜索插件、代码仓与 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 项

源码仓库

使用此插件的项目