dart-lang/fake_async

Name: fake_async

Owner: Dart

Description: null

Created: 2018-03-06 04:03:09.0

Updated: 2018-05-23 05:19:25.0

Pushed: 2018-05-01 23:20:09.0

Homepage: null

Size: 91

Language: Dart

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

This package provides a FakeAsync class, which makes it easy to deterministically test code that uses asynchronous features like Futures, Streams, Timers, and microtasks. It creates an environment in which the user can explicitly control Dart's notion of the “current time”. When the time is advanced, FakeAsync fires all asynchronous events that are scheduled for that time period without actually needing the test to wait for real time to elapse.

For example:

rt 'dart:async';

rt 'package:fake_async/fake_async.dart';
rt 'package:test/test.dart';

 main() {
st("Future.timeout() throws an error once the timeout is up", () {
// Any code run within [fakeAsync] is run within the context of the
// [FakeAsync] object passed to the callback.
fakeAsync((async) {
  // All asynchronous features that rely on timing are automatically
  // controlled by [fakeAsync].
  expect(new Completer().future.timeout(new Duration(seconds: 5)),
      throwsA(new isInstanceOf<TimeoutException>()));

  // This will cause the timeout above to fire immediately, without waiting
  // 5 seconds of real time.
  async.elapse(new Duration(seconds: 5));
});
;

Integration With clock

FakeAsync can't control the time reported by new DateTime.now() or by the Stopwatch class, since they're not part of dart:async. However, if you create them using the clock package's clock.now() or clock.getStopwatch() functions, FakeAsync will automatically override them to use the same notion of time as dart:async classes.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.