fake_async

Build Status

The fake_async package provides a FakeAsync class which allows one to fake asynchronous events such as timers and microtasks in order to test for them deterministically and without delay.

FakeAsync.run() can be used to execute the test code in a Zone which mocks out the Timer and scheduleMicrotask APIs to instead store their callbacks for execution by subsequent calls to FakeAsync.elapse() which simulates the asynchronous passage of time, which can be measured at any point via FakeAsync.elapsed. The microtask queue is drained surrounding each timer to simulate the real event queue.

For example:

import 'dart:async';

import 'package:fake_async/fake_async.dart';
import 'package:unittest/unittest.dart';

void main() {
  test("Future.timeout() throws an error once the timeout is up", () {
    new FakeAsync().run((async) {
      expect(new Completer().future.timeout(new Duration(seconds: 5)),
          throwsA(new isInstanceOf<TimeoutException>()));
      async.elapse(new Duration(seconds: 5));
    });
  });
}

##Integration with package:clock

It also integrates with the clock package so that now and getStopwatch are consistent with the fake elapsing of time. The FakeAsync constructor takes an initialTime argument to set the initial time.

Libraries

fake_async