http_mock_adapter
0.6.1, released 3 years ago by lomsa.com
A simple to use mocking package for Dio intended to be used in tests.
What it does
It provides various types and methods to declaratively mock request-response communication for the Dio HTTP client. It allows simulating server behavior and network requests in test environments.
AI-written summary of version 0.6.1, based on the package's README and metadata. Report an inaccuracy
Use it when
- You are writing tests for a Dart or Flutter project using the Dio package for networking.
- You need to declaratively mock HTTP requests and responses.
Skip it when
- You are not using Dio for networking (e.g., you use the default http package).
- You require complex production interceptors that aren't related to testing.
Strengths
- Simple to use for mocking Dio requests in tests
- Supports declarative request-response mocking
- Includes features like response delays and exact body checks
Weaknesses
- Strictly tied to the Dio package
- Limited to testing use cases
Setup
- Kind
- Pure Dart
- Effort
- Low
- Code generation
- No
- Direct dependencies
- 4
Add as a dev_dependency in pubspec.yaml and import in test files.
import 'package:dio/dio.dart';
import 'package:http_mock_adapter/http_mock_adapter.dart';
void main() async {
final dio = Dio(BaseOptions());
final dioAdapter = DioAdapter(dio: dio);
const path = 'https://example.com';
dioAdapter.onGet(
path,
(server) => server.reply(
200,
{'message': 'Success!'},
delay: const Duration(seconds: 1),
),
);
final response = await dio.get(path);
print(response.data);
}Alternatives
Packages that do the same job and could replace http_mock_adapter.
Works with
Packages commonly used together with http_mock_adapter: ones it builds on, ones built on it, and its own family.
- http_parserhttp_mock_adapter depends on itA platform-independent package for parsing and serializing HTTP formats.
- diohttp_mock_adapter depends on itA powerful HTTP networking package for Dart and Flutter with interceptors and request cancellation.
- loggerhttp_mock_adapter depends on itAn extensible logger for printing formatted logs with levels, custom filters, and printers.