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
Properties
Methods
-
fail(
AgEndpoint endpoint, AgApiException error, {AgHttpMethod method = AgHttpMethod.get}) → void -
Makes
methodrequests toendpointthrowerror, 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< headers = const {}}) → voidString> > -
Answers
methodrequests toendpointwithjson, decoded by the caller'sdecodeexactly as a real response body would be. AstatusCodeoutside 2xx throws an AgApiException carryingjsonas itsresponseBody, as the real provider does. -
replyWith(
AgEndpoint endpoint, Object? json(AgRequest request), {AgHttpMethod method = AgHttpMethod.get, int statusCode = 200, Map< String, List< headers = const {}}) → voidString> > - 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.decodeconverts the raw JSON body toD; when omitted, the raw body is used as-is (only safe whenDisdynamicor already matches the raw shape).override -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited