NetLensMocks class
Records calls as mock endpoints and replays them, so the app can be run and tested offline. It's the entry point of the mocks feature: the inspector, the HTTP client adapters and NetLensMockServer all go through it.
In NetLensMockMode.record every finished call is saved as a mock. In NetLensMockMode.replay the mock adapters of the HTTP clients answer matching requests with the saved responses. With saveAllCalls every finished call is saved whatever the mode is.
- Implemented types
Constructors
-
NetLensMocks({NetLensMockMode mode = NetLensMockMode.off, bool saveAllCalls = false, NetLensMockRepository? repository, NetLensMockMatcher matcher = const NetLensDefaultMockMatcher(), NetLensMockFallback fallback = NetLensMockFallback.network, bool simulateLatency = false, NetLensCallMockMapper? mapper, String idGenerator()?, List<
NetLensMockImporter> ? importers})
Properties
- fallback → NetLensMockFallback
-
What happens in replay mode when no mock matches a request.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- matcher → NetLensMockMatcher
-
Decides which mock answers a request.
final
-
mocks
→ List<
NetLensMock> -
Returns all mocks.
no setter
-
mocksStream
→ Stream<
List< NetLensMock> > -
Stream which returns all mocks on change.
no setter
- mode ↔ NetLensMockMode
-
Current mode.
getter/setter pair
-
modeStream
→ Stream<
NetLensMockMode> -
Stream which returns the mode on change.
no setter
- repository → NetLensMockRepository
-
Place where mocks are kept.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- saveAllCalls ↔ bool
-
Whether every finished call is saved as a mock, whatever the mode is, so
it can be replayed or exported later. Calls answered by a mock aren't
saved again. Users can change it in the NetLens settings.
getter/setter pair
-
saveAllCallsStream
→ Stream<
bool> -
Stream which returns saveAllCalls on change.
no setter
- simulateLatency → bool
-
Whether mocks wait as long as the recorded call took before answering.
final
Methods
-
clear(
) → Future< void> - Removes all mocks.
-
decode(
String content) → List< NetLensMock> -
Returns mocks of
content, in any format import reads, without saving them. Throws FormatException when the format is unknown. -
dispose(
) → void - Releases resources.
-
export(
{Iterable< NetLensMock> ? mocks}) → String -
Returns
mocks, all mocks by default, in the NetLens mocks JSON format. -
exportCollection(
{Iterable< NetLensMock> ? mocks, String name = 'NetLens'}) → String -
Returns
mocks, all mocks by default, as a Postman collection namedname. See NetLensPostmanCollectionCodec. -
findMock(
NetLensMockRequest request) → NetLensMock? -
Returns the enabled mock which answers
request, whatever the mode is. -
import(
String content) → Future< List< NetLensMock> > -
Adds mocks from
content, a document in the NetLens mocks JSON format, a HAR file or a Postman collection. Mocks of requests which already have one replace them. Returns the added mocks. Throws FormatException when the format is unknown. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
planMerge(
List< NetLensMockSource> sources, {String currentName = 'Current'}) → NetLensMockMergePlan -
Plans merging the saved mocks, as a source named
currentName, withsources, for example collections shared by teammates. Saved mocks win the default choices of conflicts. Save the result with replaceAll. -
record(
NetLensHttpCall call) → Future< NetLensMock?> -
Saves
callas a mock. Returns the mock, or null whencallhas no response which can be replayed. -
recordAll(
Iterable< NetLensHttpCall> calls) → Future<List< NetLensMock> > -
Saves every call of
callswhich can be replayed. Returns the saved mocks. -
recordFrom(
Stream< List< callsStream) → StreamSubscription<NetLensHttpCall> >List< NetLensHttpCall> > -
Records calls of
callsStreamwhile the mode is NetLensMockMode.record or saveAllCalls is true. Each call is recorded once, after it finished. -
remove(
String id) → Future< void> -
Removes the mock with
id. -
replaceAll(
Iterable< NetLensMock> mocks) → Future<List< NetLensMock> > -
Replaces all mocks with
mocks, for example the result of a merge. Saved mocks of the same request keep their id and enabled state. Returns the saved mocks. -
resolve(
NetLensMockRequest request) → Future< NetLensMockResponse?> -
Returns the response of the mock which answers
request. Returns null when mocks aren't replayed, or when no mock matches and fallback is NetLensMockFallback.network.override -
setEnabled(
String id, {required bool enabled}) → Future< void> -
Turns the mock with
idon or off. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited