我正在尝试测试一个简单的存储库类,它使用依赖注入(inject)的 Dio 包进行网络调用。 Http.post 的要求是将 Map 对象发送到 header 为 'Content-Type': 'application/json
的 URL。 .你可以在下面看到这个:
class AuthenticateUserRemoteDataSourceImpl
implements AuthenticateUserRepository {
final Dio httpClient;
AuthenticateUserRemoteDataSourceImpl({@required this.httpClient});
@override
Future<Either<Failure, AuthenticateUser>> getAuthenticateUser(
String email, String password) async {
final url = 'API_URL';
final Map<String, String> jsonPayload = {
"email": email,
"password": password
};
Response response = await httpClient.post('{$url}api/users/authenticate',
data: jsonPayload,
options: Options(headers: {'Content-Type': 'application/json'}));
}
}
我试图确保此方法包含在单元测试中,但是我很难用 Dio 包验证命名参数。我可以验证 dioClient.post
但是实际上被调用了,我在模拟“数据”和“选项”的名称参数时遇到了麻烦。
我想测试它是否被调用 Map<String, String>
对于正文,我还想测试发送的 header ,我想检查它是否使用 {'Content-Type': 'application/json'}
调用.当我需要对经过身份验证的 dio get/post/put 请求进行单元测试时,这也很有用。
这是我到目前为止的测试,如前所述,我可以验证是否调用了模拟函数,但不能验证名称参数。
group('getAuthenticateUser', () {
final tResponse = Response(statusCode: 200, data: {"title": "success"});
test(
'should perform a post request to the the API with the application/json header',
() async {
// arrange
when(dioClient.post(any,
data: anyNamed('data'), options: anyNamed('options')))
.thenAnswer((Invocation invocation) async => tResponse);
// act
await dataSource.getAuthenticateUser(tEmail, tPassword);
// assert
verify(dioClient.post(any,
data: anyNamed('data'), options: anyNamed('options')));
});
});
感谢您的帮助,我才刚刚开始进行单元测试,所以任何帮助或指点都将非常有用,
谢谢,山姆
更新
我已经实现了您的模拟类(class),效果很好,我认为这正是我所缺少的。我已经更新了我的测试,但现在不明白我哪里出错了?
测试 Dart
class MockOptions extends Mock implements Options {
final Map<String, dynamic> headers;
//also add any other parameters you want to mock as fields in this class
MockOptions({this.headers});
}
test(
'should perform a post request to the the API with the application/json header',
() async {
// arrange
Map<String, String> headers = {'Content-type': 'application/json'};
when(mockDio.post('path', options: anyNamed('options')))
.thenAnswer((_) async => Response(data: {}));
// act
dataSource.getAuthenticateUser(tEmail, tPassword);
// assert
verify(mockDio.post('path', options: MockOptions(headers: headers)));
});
方法文件如下所示:
@override
Future<Either<Failure, AuthenticateUser>> getAuthenticateUser(
String email, String password) async {
await this.httpClient.post('path',
data: {},
options: Options(headers: {'Content-type': 'application/json'}));
}
最佳答案
使用http_mock_adapter ,用于模拟 Dio
请求的新包。
您可以简单地将注入(inject)的 Dio
的 httpClientAdapter
替换为 http_mock_adapter 的 DioAdapter()
:
示例来自 examples的 http_mock_adapter
import 'package:dio/dio.dart';
import 'package:http_mock_adapter/http_mock_adapter.dart';
void main() async {
final dio = Dio();
final dioAdapter = DioAdapter();
dio.httpClientAdapter = dioAdapter;
const path = 'https://example.com';
dioAdapter
..onGet(
path,
(request) => request.reply(200, {'message': 'Successfully mocked GET!'}),
)
..onGet(
path,
(request) => request.reply(200, {'message': 'Successfully mocked POST!'}),
);
final onGetResponse = await dio.get(path);
print(onGetResponse.data); // {message: Successfully mocked GET!}
final onPostResponse = await dio.post(path);
print(onPostResponse.data); // {message: Successfully mocked POST!}
}
您还可以使用 http_mock_adapter 的 DioInterceptor
, 可以添加到 dio.interceptor
列表中。
查看 examples 中的第二个示例http_mock_adapter 的文件
关于http - 单元测试-如何在 flutter 中模拟第三方库类Dio的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61321828/