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

源码仓库

使用此插件的项目