How do I test dart NoSuchMethodError

Viewed 92

How do i test for a class that some method doesn't exists NoSuchMethodError Exists?
something like the below example.

expect(1.leftShift(12), NoSuchMethodError);
1 Answers

You can test this like you would any other error. You can pass in a function that throws the error into expect and check that it throws the right error:

const dynamic x = 'hello';
expect(() => x.notAMethod(), throwsA(isA<NoSuchMethodError>())); 

Note that you will need to make your receiver (the object you are calling the method on) to be dynamic to suppress the static error that would otherwise catch this error.

Related