Back to results for “Error Handling”
Flutter › Unit Testing in Flutter

What are matchers in Flutter testing, such as equals, isTrue, and throwsA, and how do they let you express several genuinely different kinds of specific assertions within a given test?

Beginner

Answer

A matcher is a genuinely specific object that describes exactly what condition a given actual value should genuinely satisfy, and the expect function uses these particular matchers to properly verify a wide genuine variety of different conditions, including equals for checking genuine exact equality, isTrue and isFalse for checking a genuine boolean condition, contains for checking whether a specific given collection actually genuinely includes a particular expected item, and throwsA for properly verifying that a given piece of code genuinely throws a specific expected exception type.
expect(() => divide(10, 0), throwsA(isA<ArgumentError>()));
expect([1, 2, 3], contains(2));
Real-world example A test verifying a division function correctly throws an ArgumentError specifically when dividing by zero uses the throwsA matcher combined with isA to precisely verify both that a genuine exception was actually thrown and that it was genuinely the exact correct expected specific type.

Common follow-ups: What genuinely other useful built in matchers does the flutter_test package already provide beyond these particular commonly used ones?;Can you actually genuinely write your own fully custom matcher for a genuinely specific particular need not already covered by the existing built in ones?