use_test_throws_matchers
fail() の代わりに throwsA マッチャーを使用してください。
詳細
#fail() を使用した try-catch の代わりに throwsA マッチャーを使用してください。
悪い例
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 > rules の下に use_test_throws_matchers: true を追加してください。
analysis_options.yaml
yaml
linter:
rules:
use_test_throws_matchers: true