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);
});

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()

Properties

cancellations List<String>
Reasons passed to cancelRequest, in order.
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 cancellationReason = ''}) → void
Cancels all in-flight requests.
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, 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, 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, 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, bool showLoader = true, CachePolicy? cachePolicy, Duration? cacheMaxAge, 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, bool showLoader = true, CachePolicy? cachePolicy, Duration? cacheMaxAge, bool dedupe = true}) Future<Either<Failure, T>>
get, with the response body deserialized into T.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
patch({required String endpoint, Object? data, Map<String, dynamic>? params, Duration? receiveTimeout, Duration? sendTimeout, Map<String, String>? headers, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, CancelToken? cancelToken, 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, 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, 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, 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, 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, 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 successful JSON response — the common case.
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, 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