test icon indicating copy to clipboard operation
test copied to clipboard

Add `throwsTypeError` to `throws_matchers.dart`

Open ChopinDavid opened this issue 2 years ago • 0 comments

In my flutter tests, I am wanting to be able to assert that a TypeError was thrown. To do this, I have to do something like:

test('fromJson throws TypeError when "key" is of type Map instead of String', () {
    expect(
        () => Object.fromJson({
            'key': {},
        }),
        throwsA(isA<TypeError>()),
    );
});

The above test should also be able to be written as so:

test('fromJson throws TypeError when "key" is of type Map instead of String', () {
    expect(
        () => Object.fromJson({
            'key': {},
        }),
        throwsTypeError),
    );
});

Similar matchers are already supported, as seen here.

ChopinDavid avatar Mar 22 '24 02:03 ChopinDavid