Can I make a custom matcher for Flutter unit or integration tests?

Issue

I’m writing a unit or integration test for mobile. Can I make a custom matcher?

Solution

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.

Answered By – user1704813

Answer Checked By – Mildred Charles (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.