FakeAsync class abstract
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', () {
FakeAsync().run((async) {
testedFunc(clock: async.getClock(initialTime));
async.elapse(duration);
expect(...)
});
});
Constructors
- FakeAsync()
-
factory
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- microtaskCount → int
-
The number of pending microtasks.
no setter
- nonPeriodicTimerCount → int
-
The number of pending non periodic timers that have not been canceled.
no setter
-
pendingTimersDebugInfo
→ List<
String> -
Debugging information for all pending timers.
no setter
- periodicTimerCount → int
-
The number of created periodic timers that have not been canceled.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
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.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
run(
dynamic callback(FakeAsync self)) → dynamic -
Runs
callback
in a Zone with fake timer and microtask scheduling. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited