dartdart-asyncdart-test

How to create a matcher that verifies an async callback is doing right thing?


I would like to create a matcher that takes async or non-async callback, sets up some listeners, invokes callback, verifies listeners received expected events, and then clean up listeners.

However, I see couple blockers:

  1. The method Matcher.matches is sync
  2. It seems expectLater is taking result of invocation, so matcher cannot configure listeners

Is it possible at all to create what I want?


Solution

  • I ended up creating a helper async method events that takes the callback as parameter, configures listeners, invokes callback, cleans up listeners, an returns the list of the collected events. And then matcher analyses the list synchronously:

    await expectLater(
        await events(() async {...}), areInRightSequence,
    );