use_test_throws_matchers
使用 throwsA 匹配器而非 fail()。
詳情
#使用 throwsA 匹配器而非 try-catch 結合 fail()。
不良示例
dart
// sync code
try {
someSyncFunctionThatThrows();
fail('expected Error');
} on Error catch (error) {
expect(error.message, contains('some message'));
}
// async code
try {
await someAsyncFunctionThatThrows();
fail('expected Error');
} on Error catch (error) {
expect(error.message, contains('some message'));
}推薦示例
dart
// sync code
expect(
() => someSyncFunctionThatThrows(),
throwsA(isA<Error>().having((Error error) => error.message, 'message', contains('some message'))),
);
// async code
await expectLater(
() => someAsyncFunctionThatThrows(),
throwsA(isA<Error>().having((Error error) => error.message, 'message', contains('some message'))),
);啟用
#要啟用 use_test_throws_matchers 規則,請在您的 analysis_options.yaml 檔案中的 linter > rules 下新增 use_test_throws_matchers
analysis_options.yaml
yaml
linter:
rules:
- use_test_throws_matchers如果您使用 YAML 對映語法配置 linter 規則,請在 linter > rules 下新增 use_test_throws_matchers: true
analysis_options.yaml
yaml
linter:
rules:
use_test_throws_matchers: true