NyMockApi class

API mocking utilities for testing.

This class allows you to mock API responses using type-based handlers or URL pattern matching with wildcards.

Example:

// Mock by API service type
NyMockApi.register<UserApiService>((request) async {
  return Response(data: {'id': 1, 'name': 'Test User'});
});

// Mock by URL pattern
NyMockApi.respond('/users/*', {'id': 1, 'name': 'Test User'});
NyMockApi.respond('/posts/**', {'posts': []});

Constructors

NyMockApi()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

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

Static Properties

handlerCount int
Get count of registered handlers.
no setter
patternCount int
Get count of registered patterns.
no setter

Static Methods

callCount(String endpoint, {String? method}) int
Get the number of times an endpoint was called.
clear() → void
Clear all mocks and history.
clearHandlers() → void
Clear only type handlers.
clearHistory() → void
Clear only call history.
clearPatterns() → void
Clear only URL patterns.
createResponse<T>({required T data, int statusCode = 200, String statusMessage = 'OK', Map<String, List<String>>? headers, RequestOptions? requestOptions}) → Response<T>
Create a mock Dio response.
getCalls() List<ApiCallInfo>
Get all recorded calls.
getCallsFor(String endpoint) List<ApiCallInfo>
Get calls for a specific endpoint.
getCallsForType<T>() List<ApiCallInfo>
Get calls for a specific API type.
getHandler<T>() MockApiHandler?
Get the handler for a specific type.
getHandlerByType(Type type) MockApiHandler?
Get handler by type.
hasHandler<T>() bool
Check if a type has a registered handler.
matchUrl(String url, {String method = 'GET'}) MockResponse?
Match a URL against registered patterns. Matches against both the full URL and just the path portion.
recordCall(String endpoint, {String method = 'GET', dynamic data, Map<String, dynamic>? headers}) → void
Record an API call for later assertions.
register<T>(MockApiHandler handler) → void
Register a mock handler for a specific API service type.
respond(String pattern, dynamic response, {int statusCode = 200, String method = 'GET', Map<String, dynamic>? headers, Duration? delay}) → void
Register a URL pattern with a mock response.
setRecordCalls(bool record) → void
Enable or disable call recording.
wasCalled(String endpoint, {String? method, int? times}) bool
Check if an endpoint was called.