Skip to content

Files

Latest commit

 

History

History
48 lines (36 loc) · 1002 Bytes

use_test_throws_matchers.md

File metadata and controls

48 lines (36 loc) · 1002 Bytes

Pattern: Missing use of throwsA matcher

Issue: -

Description

Use the throwsA matcher instead of try-catch with fail().

Example of incorrect code:

// 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'));
}

Example of correct code:

// 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'))),
);

Further Reading