0 purchases
dio mocked responses
Flutter Dio Mock Interceptor #
Forked from Flutter Dio Mock Interceptor
Environment #
The widget was only tested on following environment,
Flutter: 3.24+ (with sound null safety)
Dio: 5.0.0+
Usage #
Add interceptor to Dio
final dio = Dio()
..interceptors.add(
MockInterceptor(
basePath: 'test/dio_responses',
),
);
copied to clipboard
basePath is the path to the folder containing the mock responses. The path is relative to the root of the assets folder.
His default value is test/dio_responses.
By example, if you want to test your backend API for the route api/client/55036c03-6d3f-4053-9547-c08a32ac9aca/contacts, create the file at test/dio_responses/api/client/55036c03-6d3f-4053-9547-c08a32ac9aca/contacts.json with:
{
"GET": {
"statusCode": 200,
"data": {
"contacts": [
{
"id": 1,
"name": "Seth Ladd"
},
{
"id": 2,
"name": "Eric Seidel"
}
]
}
}
}
copied to clipboard
For this example:
test('Load file with Interceptor', () async {
final dio = Dio()
..interceptors.add(
MockInterceptor(basePath: 'test/dio_responses'),
);
final response = await dio.get<Map<String, dynamic>>(
'api/client/55036c03-6d3f-4053-9547-c08a32ac9aca/contacts',
);
expect(response.statusCode, equals(200));
expect(response.data, isNotNull);
final contacts = response.data!['contacts'];
final seth = contacts.first;
expect(seth['id'], 1);
expect(seth['name'], 'Seth Ladd');
final eric = contacts.last;
expect(eric['id'], 2);
expect(eric['name'], 'Eric Seidel');
});
copied to clipboard
Template example:
{
"POST": {
"statusCode": 200,
"template": {
"size": 100000,
"content": {
"id": "test${index}",
"name": "name_${index}"
}
}
}
}
copied to clipboard
License #
This project is licensed under the MIT License - see the LICENSE file for details.
Copyright (c) 2024-present Listo Paye
Copyright (c) 2023-present Yong-Xin Technology Ltd.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.