Seth Ladd Seth Ladd - 10 months ago 72
Dart Question

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

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download