Unit testing with FakeAsync in Dart

Viewed 627

I'm kind of new to unit testing, and I am writing a unit test which uses the quiver package for faking async execution. The function I am using looks something like this:

import 'dart:async';

import 'package:quiver/testing/async.dart';
import 'package:rxdart/rxdart.dart';
import 'package:test/test.dart' as tester;

testStream<MyType>(
  String message, {
  Iterable expecting,
  Future<void> Function(BehaviorSubject) action,
  Duration waitTime,
  int skip = 1,
}) {
  tester.test(message, () {
    final _stream = BehaviorSubject<MyType>();
    FakeAsync().run((async) async {
      final List<MyType> _results = <MyType>[];
      final _subscription = _stream.skip(skip).listen(_results.add);
      await action?.call(_stream);

      if (waitTime != null) async.elapse(waitTime);

      _subscription.cancel();
      if (expecting != null) {
        tester.expect(_results, expecting);
      }
    });
  });
}

It works well on most tests I wrote, but I am having problems with a specific example due to the lack of knowledge. In the particular case I pass the action which adds a delayed future to the stream similarly like this:

testStream<int>(
  'Just a basic message',
  expecting: [1],
  action: (stream) async =>
       Future.delayed(Duration(days: 99), () => stream.add(1)),
  waitTime: Duration(seconds: 1),
);

If I understood it right, this test should fail, however when I run it, it passes. What am I doing wrong here?

Then, what should I do to make it execute the action first and then elapse the timer after the action has been called without waiting for it?

0 Answers
Related