'How to unit test Stream.listen() in Dart

Consider the following test code ...

main() {
  StreamController controller;
  setUp(() {
    controller = StreamController.broadcast(sync: false);
  });

  tearDown(() {
    controller.close();
  });

  test('Stream listen test', () {
    var stream = controller.stream;

    stream.listen((event) {
      // >>>>> How can I test if this was called?? <<<<<<
    });

    controller.add('Here is an event!');
  });
}

... what is the correct way to test that the stream.listen(...) call-back was invoked?

stream.listen(...) doesn't return a Future so traditional expectAsyncX style matchers will not work here.

Perhaps there is some way to use the StreamSubscription.asFuture() that is returned by stream.listen(...)?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source