dio_mocked_responses 1.5.0+1
dio_mocked_responses: ^1.5.0+1 copied to clipboard
Interceptor that help you to mock backend responses in flutter project.
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',
),
);
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"
}
]
}
}
}
- 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');
});
- Template example:
{
"POST": {
"statusCode": 200,
"template": {
"size": 100000,
"content": {
"id": "test${index}",
"name": "name_${index}"
}
}
}
}
Get history #
You can get the history of requests with the history property of the interceptor.
Future<void> aCallWasMadeWithData(
WidgetTester tester,
String verb,
String path,
bdd.DataTable dataTable,
) async {
expect(
MockInterceptor.history.where((p) => p.method == verb && p.path == path),
hasLength(1));
}
Contextualised responses #
You can contextualize the responses by adding the context parameter to the interceptor.
Future<void> myContextIs(WidgetTester tester, String ctx) async {
MockInterceptor.setContext(ctx);
}
When you set the context, the interceptor will look for the file with the same name as the context in the folder of the response.
For example, if you set the context as 'test', the interceptor will look for the file test/dio_responses/api/client/55036c03-6d3f-4053-9547-c08a32ac9aca/contacts/test.json.
If 'test' is not found, the interceptor will look for the file test/dio_responses/api/client/55036c03-6d3f-4053-9547-c08a32ac9aca/contacts.json.
To remove the context, you can call MockInterceptor.clearContext().
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.