AgFakeApiProvider class

An ApiProvider for tests: it answers from replies set up in advance and never touches the network.

Register it in place of the real provider and the whole stack above it — Service, Repo, Controller — runs unchanged:

final api = AgFakeApiProvider()
  ..reply(TaskEndpoints.tasks, json: [{'id': 1, 'title': 'Write tests'}])
  ..fail(
    TaskEndpoints.task,
    const AgApiException(message: 'offline', type: AgApiErrorType.connection),
  );
AgLocator.put<ApiProvider>(api);

// ... run the code under test, then:
expect(api.requests.single.queryParams, {'_start': 0, '_limit': 20});

A reply is matched by the endpoint's path template and the HTTP method, so one reply covers every id. The latest reply for a pair wins, which lets a test change the answer halfway through. A request with no reply throws a StateError naming it — a missing reply is a bug in the test, not an API error the code under test should handle.

Implemented types

Constructors

AgFakeApiProvider()

Properties

hashCode → int
The hash code for this object.
no setterinherited
requests → List<AgRequest>
Every request sent so far, oldest first.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

fail(AgEndpoint endpoint, AgApiException error, {AgHttpMethod method = AgHttpMethod.get}) → void
Makes method requests to endpoint throw error, the way the real provider reports a failed request.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
reply(AgEndpoint endpoint, {AgHttpMethod method = AgHttpMethod.get, Object? json, int statusCode = 200, Map<String, List<String>> headers = const {}}) → void
Answers method requests to endpoint with json, decoded by the caller's decode exactly as a real response body would be. A statusCode outside 2xx throws an AgApiException carrying json as its responseBody, as the real provider does.
replyWith(AgEndpoint endpoint, Object? json(AgRequest request), {AgHttpMethod method = AgHttpMethod.get, int statusCode = 200, Map<String, List<String>> headers = const {}}) → void
Like reply, but builds the body from each request — to echo a posted body back, or to answer per path parameter.
send<D>(AgRequest request, {D decode(Object? json)?, AgCancelToken? cancelToken, void onSendProgress(int, int)?, void onReceiveProgress(int, int)?}) → Future<AgResponse<D>>
Sends request, returning a decoded AgResponse or throwing an AgApiException. decode converts the raw JSON body to D; when omitted, the raw body is used as-is (only safe when D is dynamic or already matches the raw shape).
override
toString() → String
A string representation of this object.
inherited

Operators

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