FakeNetworkDriver class

A fake network driver for testing.

Records all requests and allows stubbing responses via URL patterns, maps, or callback handlers. Inspired by Laravel's Http::fake().

final fake = FakeNetworkDriver();
fake.stub('users/*', MagicResponse(data: {'id': 1}, statusCode: 200));

final response = await fake.get('/users/42');
fake.assertSent((r) => r.url.contains('users'));
Implemented types

Constructors

FakeNetworkDriver({dynamic stubs})
Creates a FakeNetworkDriver.

Properties

hashCode int
The hash code for this object.
no setterinherited
recorded List<(MagicRequest, MagicResponse)>
Recorded request/response pairs.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

addInterceptor(MagicNetworkInterceptor interceptor) → void
Add an interceptor to the driver.
override
assertNothingSent() → void
Assert that no requests were recorded at all.
assertNotSent(bool predicate(MagicRequest request)) → void
Assert that no recorded request matches predicate.
assertSent(bool predicate(MagicRequest request)) → void
Assert that at least one recorded request matches predicate.
assertSentCount(int expected) → void
Assert that exactly expected requests were recorded.
delete(String url, {Map<String, String>? headers}) Future<MagicResponse>
Perform a DELETE request.
override
destroy(String resource, String id, {Map<String, String>? headers}) Future<MagicResponse>
Delete a resource (DELETE /resource/{id}).
override
get(String url, {Map<String, dynamic>? query, Map<String, String>? headers}) Future<MagicResponse>
Perform a GET request.
override
index(String resource, {Map<String, dynamic>? filters, Map<String, String>? headers}) Future<MagicResponse>
Fetch all resources (GET /resource).
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
post(String url, {dynamic data, Map<String, String>? headers}) Future<MagicResponse>
Perform a POST request.
override
preventStrayRequests() FakeNetworkDriver
Enable strict mode — unmatched requests throw StrayRequestException.
put(String url, {dynamic data, Map<String, String>? headers}) Future<MagicResponse>
Perform a PUT request.
override
reset() → void
Reset all recorded requests for reuse.
show(String resource, String id, {Map<String, String>? headers}) Future<MagicResponse>
Fetch a single resource (GET /resource/{id}).
override
store(String resource, Map<String, dynamic> data, {Map<String, String>? headers}) Future<MagicResponse>
Create a new resource (POST /resource).
override
stub(String urlPattern, MagicResponse response) FakeNetworkDriver
Register a URL pattern stub.
toString() String
A string representation of this object.
inherited
update(String resource, String id, Map<String, dynamic> data, {Map<String, String>? headers}) Future<MagicResponse>
Update a resource (PUT /resource/{id}).
override
upload(String url, {required Map<String, dynamic> data, required Map<String, dynamic> files, Map<String, String>? headers}) Future<MagicResponse>
Upload files via multipart form data.
override

Operators

operator ==(Object other) bool
The equality operator.
inherited