dart-checks-migration
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDart Checks Migration
Dart Checks 迁移指南
When to use this skill
何时使用此技能
Use this skill when:
- Migrating existing test files from to
package:matcher.package:checks - A user specifically asks for "modern checks" or similar.
在以下场景使用此技能:
- 将现有测试文件从迁移到
package:matcher时。package:checks - 用户明确要求“现代化检查”或类似需求时。
The Workflow
工作流程
- Analysis:
- Use to identify files using
greporexpect.package:matcher - Review custom matchers; these may require manual migration.
- Use
- Tools & Dependencies:
- Ensure includes
dev_dependencies.checks - Run if missing.
dart pub add --dev checks
- Ensure
- Discovery:
- Use the Strategies for Discovery below to find candidates.
- Replacement:
- Add .
import 'package:checks/checks.dart'; - Apply the Common Patterns below.
- Final Step: Replace with
import 'package:test/test.dart';ONLY after allimport 'package:test/scaffolding.dart';calls are replaced. This ensures incremental progress.expect
- Add
- Verification:
- Ensure the code analyzes cleanly.
- Ensure tests pass.
- 分析:
- 使用识别使用
grep或expect的文件。package:matcher - 检查自定义匹配器;这些可能需要手动迁移。
- 使用
- 工具与依赖:
- 确保中包含
dev_dependencies。checks - 如果缺失,运行。
dart pub add --dev checks
- 确保
- 发现候选:
- 使用下方的发现策略找到需要迁移的代码。
- 替换操作:
- 添加。
import 'package:checks/checks.dart'; - 应用下方的常见模式进行替换。
- 最后一步:仅在所有调用都替换完成后,将
expect替换为import 'package:test/test.dart';。这可确保迁移过程逐步推进。import 'package:test/scaffolding.dart';
- 添加
- 验证:
- 确保代码能通过静态分析。
- 确保测试用例全部通过。
Common Patterns
常见模式
Legacy | Modern |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
旧版 | 新版 |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
Async & Futures (CRITICAL)
异步与Future(重点注意)
-
Checking async functions:causes FALSE POSITIVES because the closure returns a
check(() => asyncFunc()).throws<T>(), which is a value, so it "completes normally" (as a Future). Correct Usage:Futuredartawait check(asyncFunc()).throws<T>(); -
Chaining on void returns: Many async check methods (like) return
throws. You cannot chain directly on them. Use cascades or callbacks. Wrong:Future<void>dartawait check(future).throws<Error>().has((e) => e.message, 'message').equals('foo');Correct:dartawait check(future).throws<Error>((it) => it.has((e) => e.message, 'message').equals('foo'));
-
检查异步函数:会导致误判,因为闭包返回的是
check(() => asyncFunc()).throws<T>()对象,它会“正常完成”(作为一个Future)。 正确用法:Futuredartawait check(asyncFunc()).throws<T>(); -
无返回值的链式调用: 许多异步检查方法(如)返回
throws。你不能直接在它们后面链式调用其他方法。请使用级联操作或回调函数。 错误写法:Future<void>dartawait check(future).throws<Error>().has((e) => e.message, 'message').equals('foo');正确写法:dartawait check(future).throws<Error>((it) => it.has((e) => e.message, 'message').equals('foo'));
Complex Examples
复杂示例
Deep Verification with and :
isAhavingLegacy:
dart
expect(() => foo(), throwsA(isA<ArgumentError>()
.having((e) => e.message, 'message', contains('MSG'))));Modern:
dart
check(() => foo())
.throws<ArgumentError>()
.has((e) => e.message, 'message')
.contains('MSG');Property Extraction:
Legacy:
dart
expect(obj.prop, equals(value)); // When checking multiple propsModern:
dart
check(obj)
..has((e) => e.prop, 'prop').equals(value)
..has((e) => e.other, 'other').equals(otherValue);One-line Cascades:
Since checks often return , use cascades for multiple assertions on the
same subject.
voiddart
check(it)..isGreaterThan(10)..isLessThan(20);使用和进行深度验证:
isAhaving旧版:
dart
expect(() => foo(), throwsA(isA<ArgumentError>()
.having((e) => e.message, 'message', contains('MSG'))));新版:
dart
check(() => foo())
.throws<ArgumentError>()
.has((e) => e.message, 'message')
.contains('MSG');属性提取:
旧版:
dart
expect(obj.prop, equals(value)); // 当检查多个属性时新版:
dart
check(obj)
..has((e) => e.prop, 'prop').equals(value)
..has((e) => e.other, 'other').equals(otherValue);单行级联调用:
由于check方法通常返回,对同一对象进行多个断言时请使用级联操作。
voiddart
check(it)..isGreaterThan(10)..isLessThan(20);Constraints
约束条件
- Scope: Only modify files in (and
test/).pubspec.yaml - Correctness: One failing test is unacceptable. If a test fails after migration and you cannot fix it immediately, REVERT that specific change.
- Type Safety: is stricter about types than
package:checks. You may need to add explicitmatchercasts oras Tchecks in the chain.isA<T>()
- 范围:仅修改目录下的文件(以及
test/)。pubspec.yaml - 正确性:不允许出现测试失败的情况。如果迁移后某个测试失败且无法立即修复,请撤销该特定修改。
- 类型安全:在类型检查上比
package:checks更严格。你可能需要在链式调用中添加显式的matcher类型转换或as T检查。isA<T>()