I'm writing a unit or integration test for mobile. Can I make a custom matcher?
Yes, you can create custom Matcher
classes and custom Finder
classes.
Here's one of Flutter's custom matchers that asserts that the given Finder
finds a widget that's inside a Card
widget:
class _IsInCard extends Matcher {
const _IsInCard();
@override
bool matches(covariant Finder finder, Map<dynamic, dynamic> matchState) => _hasAncestorOfType(finder, Card);
@override
Description describe(Description description) => description.add('in card');
}
The Matcher
class is not part of the Flutter framework. It is defined in package:mathcer
. Flutter inherits it from package:test
. All Flutter's own Matcher classes are custom implementations of it.
Flutter's test framework has lots of custom matchers and custom finders you can learn from.