FakeAsync class

A mechanism to make time-dependent units testable.

Test code can be passed as a callback to run, which causes it to be run in a Zone which fakes timer and microtask creation, such that they are run during calls to elapse which simulates the asynchronous passage of time.

The synchronous passage of time (blocking or expensive calls) can also be simulated using elapseBlocking.

To allow the unit under test to tell time, it can receive a Clock as a dependency, and default it to const Clock() in production, but then use clock in test code.

Example:

test('testedFunc', () {
  new FakeAsync().run((async) {
    testedFunc(clock: async.getClock(initialTime));
    async.elapse(duration);
    expect(...)
  });
});

Constructors

FakeAsync()
factory

Properties

microtaskCount int
The number of pending microtasks.
read-only
nonPeriodicTimerCount int
The number of pending non periodic timers that have not been canceled.
read-only
pendingTimersDebugInfo List<String>
Debugging information for all pending timers. [...]
read-only
periodicTimerCount int
The number of created periodic timers that have not been canceled.
read-only
hashCode int
The hash code for this object. [...]
read-only, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

elapse(Duration duration) → void
Simulates the asynchronous passage of time. [...]
elapseBlocking(Duration duration) → void
Simulates the synchronous passage of time, resulting from blocking or expensive calls. [...]
flushMicrotasks() → void
Runs all remaining microtasks, including those scheduled as a result of running them, until there are no more microtasks scheduled. [...]
flushTimers({Duration timeout: const Duration(hours: 1), bool flushPeriodicTimers: true }) → void
Runs all timers until no timers remain (subject to flushPeriodicTimers option), including those scheduled as a result of running them. [...]
getClock(DateTime initialTime) Clock
Returns a fake Clock whose time can is elapsed by calls to elapse and elapseBlocking. [...]
run(dynamic callback(FakeAsync self)) → dynamic
Runs callback in a Zone with fake timer and microtask scheduling. [...]
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
toString() String
Returns a string representation of this object.
inherited

Operators

operator ==(dynamic other) bool
The equality operator. [...]
inherited