FakeApiServices class

An in-memory ApiServices for tests, so you can exercise a repository or controller without mocking Dio or standing up a server.

import 'package:baaba_api_handler/testing.dart';

late FakeApiServices api;

setUp(() {
  api = FakeApiServices();
  api.stubJson(HttpMethod.get, '/users/1', {'id': 1, 'name': 'Ada'});
});

test('loads the user', () async {
  final repo = UserRepository(api);

  expect(await repo.find(1), isA<User>());
  expect(api.recordedCalls.single.endpoint, '/users/1');
});

test('surfaces a server error', () async {
  api.stubFailure(HttpMethod.get, '/users/1', statusCode: 500);

  expect(await repo.find(1), isNull);
});

It behaves like the real client where that shapes an outcome: ApiConfig.isSuccess, an already-cancelled CancelToken, and ApiConfig.observer all apply exactly as they do in production.

A missing stub throws. Calling an endpoint you did not stub raises a StateError naming the method and endpoint, so an unexpected call fails loudly instead of quietly returning null and failing three asserts later.

Implemented types

Constructors

FakeApiServices({String client = ApiServices.defaultClient})

Properties

cancellations → List<String>
Reasons passed to cancelRequest, in order.
final
cancelledTags → List<String?>
The tag passed to each cancelRequest call, in order — null for a call that cancelled everything. Parallel to cancellations.
final
client → String
The client whose ApiConfig this fake answers to.
final
hashCode → int
The hash code for this object.
no setterinherited
recordedCalls → List<RecordedCall>
Every call made so far, in order.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

cancelRequest({String? tag, String cancellationReason = ''}) → void
Cancels in-flight requests — every one of them, or just those made under a tag.
override
delete({required String endpoint, Object? data, Map<String, dynamic>? params, Duration? receiveTimeout, Duration? sendTimeout, Map<String, String>? headers, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, CancelToken? cancelToken, ResponseType? responseType, String? tag, bool? retryable, Map<String, dynamic>? extra, bool showLoader = true}) → Future<Either<Failure, Response>>
Sends a DELETE request to endpoint.
override
deleteAs<T>({required String endpoint, required T parser(dynamic data), Object? data, Map<String, dynamic>? params, Duration? receiveTimeout, Duration? sendTimeout, Map<String, String>? headers, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, CancelToken? cancelToken, ResponseType? responseType, String? tag, bool? retryable, Map<String, dynamic>? extra, bool showLoader = true}) → Future<Either<Failure, T>>
delete, with the response body deserialized into T. See getAs.
override
download({required String endpoint, required String savePath, Map<String, dynamic>? params, Duration? receiveTimeout, Duration? sendTimeout, Map<String, String>? headers, ProgressCallback? onReceiveProgress, CancelToken? cancelToken, String? tag, bool? retryable, Map<String, dynamic>? extra, bool deleteOnError = true, bool showLoader = true}) → Future<Either<Failure, Response>>
Downloads the file at endpoint and streams it directly to savePath, instead of loading the whole response into memory like get would.
override
get({required String endpoint, Object? data, Map<String, dynamic>? params, Duration? receiveTimeout, Duration? sendTimeout, Map<String, String>? headers, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, CancelToken? cancelToken, ResponseType? responseType, String? tag, bool? retryable, Map<String, dynamic>? extra, bool showLoader = true, bool dedupe = true}) → Future<Either<Failure, Response>>
Sends a GET request to endpoint.
override
getAs<T>({required String endpoint, required T parser(dynamic data), Object? data, Map<String, dynamic>? params, Duration? receiveTimeout, Duration? sendTimeout, Map<String, String>? headers, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, CancelToken? cancelToken, ResponseType? responseType, String? tag, bool? retryable, Map<String, dynamic>? extra, bool showLoader = true, bool dedupe = true}) → Future<Either<Failure, T>>
get, with the response body deserialized into T.
override
Sends a HEAD request to endpoint.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
options({required String endpoint, Map<String, dynamic>? params, Duration? receiveTimeout, Duration? sendTimeout, Map<String, String>? headers, CancelToken? cancelToken, ResponseType? responseType, String? tag, bool? retryable, Map<String, dynamic>? extra, bool showLoader = true, bool dedupe = true}) → Future<Either<Failure, Response>>
Sends an OPTIONS request to endpoint.
override
patch({required String endpoint, Object? data, Map<String, dynamic>? params, Duration? receiveTimeout, Duration? sendTimeout, Map<String, String>? headers, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, CancelToken? cancelToken, ResponseType? responseType, String? tag, bool? retryable, Map<String, dynamic>? extra, bool showLoader = true}) → Future<Either<Failure, Response>>
Sends a PATCH request to endpoint.
override
patchAs<T>({required String endpoint, required T parser(dynamic data), Object? data, Map<String, dynamic>? params, Duration? receiveTimeout, Duration? sendTimeout, Map<String, String>? headers, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, CancelToken? cancelToken, ResponseType? responseType, String? tag, bool? retryable, Map<String, dynamic>? extra, bool showLoader = true}) → Future<Either<Failure, T>>
patch, with the response body deserialized into T. See getAs.
override
post({required String endpoint, Object? data, Map<String, dynamic>? params, Duration? receiveTimeout, Duration? sendTimeout, Map<String, String>? headers, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, CancelToken? cancelToken, ResponseType? responseType, String? tag, bool? retryable, Map<String, dynamic>? extra, bool showLoader = true}) → Future<Either<Failure, Response>>
Sends a POST request to endpoint.
override
postAs<T>({required String endpoint, required T parser(dynamic data), Object? data, Map<String, dynamic>? params, Duration? receiveTimeout, Duration? sendTimeout, Map<String, String>? headers, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, CancelToken? cancelToken, ResponseType? responseType, String? tag, bool? retryable, Map<String, dynamic>? extra, bool showLoader = true}) → Future<Either<Failure, T>>
post, with the response body deserialized into T. See getAs.
override
put({required String endpoint, Object? data, Map<String, dynamic>? params, Duration? receiveTimeout, Duration? sendTimeout, Map<String, String>? headers, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, CancelToken? cancelToken, ResponseType? responseType, String? tag, bool? retryable, Map<String, dynamic>? extra, bool showLoader = true}) → Future<Either<Failure, Response>>
Sends a PUT request to endpoint.
override
putAs<T>({required String endpoint, required T parser(dynamic data), Object? data, Map<String, dynamic>? params, Duration? receiveTimeout, Duration? sendTimeout, Map<String, String>? headers, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, CancelToken? cancelToken, ResponseType? responseType, String? tag, bool? retryable, Map<String, dynamic>? extra, bool showLoader = true}) → Future<Either<Failure, T>>
put, with the response body deserialized into T. See getAs.
override
reset() → void
Clears stubs, recorded calls, and cancellations.
override
stub(HttpMethod method, String endpoint, Either<Failure, Response> result) → void
Queues result for the next call to method endpoint.
stubDownload(String endpoint, {int statusCode = 200}) → void
Stubs a download call, which is recorded under HttpMethod.get.
stubFailure(HttpMethod method, String endpoint, {Failure? failure, int statusCode = 400, Object? body, String? message}) → void
Stubs a failure.
stubJson(HttpMethod method, String endpoint, Object? body, {int statusCode = 200, Map<String, List<String>>? headers}) → void
Stubs a JSON response — usually a successful one.
stubOffline(HttpMethod method, String endpoint) → void
Stubs the offline short-circuit, for testing no-connection paths.
toString() → String
A string representation of this object.
inherited
upload({required String endpoint, Map<String, dynamic> fields = const {}, List<UploadFile> files = const [], HttpMethod method = HttpMethod.post, Map<String, dynamic>? params, Duration? receiveTimeout, Duration? sendTimeout, Map<String, String>? headers, ProgressCallback? onSendProgress, CancelToken? cancelToken, ResponseType? responseType, String? tag, bool? retryable, Map<String, dynamic>? extra, bool showLoader = true}) → Future<Either<Failure, Response>>
Uploads files as a multipart request, alongside any ordinary form fields.
override

Operators

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