FakeNetworkHandler class
In-memory NetworkHandler double for unit-testing repositories and cubits without Dio, adapters, or a registry.
Queue the results your test expects, in call order:
final fake = FakeNetworkHandler();
final repo = UserRepository(fake, FakeApi());
fake.enqueueOk(UserDto(id: 1));
expect((await repo.getUser(1)).isOk, isTrue);
fake.enqueueErr(const UserNotFoundFailure());
expect((await repo.getUser(2)).failureOrNull, isA<UserNotFoundFailure>());
safeCall never runs the request closure — the queued result is returned
directly. An empty queue throws a descriptive StateError unless
defaultResult is set (useful for "every call fails offline" tests).
- Inheritance
-
- Object
- NetworkHandler
- FakeNetworkHandler
Constructors
- FakeNetworkHandler({Result? defaultResult})
Properties
- callCount ↔ int
-
Number of
safeCallinvocations so far.getter/setter pair - defaultResult ↔ Result?
-
Returned when the queue is empty;
nullmeans an empty queue throws.getter/setter pair - enableDebugLogging ↔ bool
-
Whether caught errors are logged with
debugPrint.getter/setter pairinherited - errorRegistry → ErrorRegistry
-
The registry consulted for every mapping decision of this handler.
finalinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- onFailure ↔ FailureObserver?
-
Called for every Err this handler produces from a caught exception.
Exceptions thrown by the observer itself are swallowed — observability
must never break error propagation.
getter/setter pairinherited
- remaining → int
-
Results queued but not yet consumed.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
enqueue(
Result result) → void -
Queues
resultfor the next call. -
enqueueErr(
Failure failure) → void - Queues a failure.
-
enqueueOk(
Object? value) → void -
Queues a success carrying
value. -
enqueueOkAll(
Iterable< Object?> values) → void -
Queues successes carrying each of
values, in order. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
reset(
) → void -
Drops everything queued and resets callCount — for
setUp/tearDownwhen one fake is shared across tests. -
safeCall<
T> (Future< T> request(), {Result<T> ? onEndpointError(DioException)?}) → Future<Result< T> > -
Executes
requestand maps any thrown error to an Err via the registry. SeeSimpleNetworkHandler.safeCallfor the historical docs; semantics are identical.override -
toString(
) → String -
A string representation of this object.
inherited
-
verifyDrained(
) → void - Throws when results are still queued — i.e. the code under test made fewer calls than the test expected. Put it at the end of a test:
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited