http - 单元测试-如何在 flutter 中模拟第三方库类Dio的参数

标签 http flutter dart mockito flutter-test

我正在尝试测试一个简单的存储库类,它使用依赖注入(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')));
    });
  });

感谢您的帮助,我才刚刚开始进行单元测试,所以任何帮助或指点都将非常有用,

谢谢,山姆

@爱 enter image description here

更新

我已经实现了您的模拟类(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)的 DiohttpClientAdapter 替换为 http_mock_adapterDioAdapter() :

示例来自 exampleshttp_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_adapterDioInterceptor , 可以添加到 dio.interceptor 列表中。

查看 examples 中的第二个示例http_mock_adapter 的文件

关于http - 单元测试-如何在 flutter 中模拟第三方库类Dio的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61321828/

相关文章:

http - 浏览器缓存如何判断请求是否相同?

android - 带有 HttpURLConnection 的 HTTP post 文件

dart - 在 Flutter 中导航屏幕时丢失数据

dart - 使用镜像生成动态表

ios8 - Paper Elements Dart安装

javascript - 如何知道请求是使用 HTTP 1.x 还是 HTTP2 执行的

javascript - 设置 'Content-disposition' 而不设置 'Content-Length' 是否可以接受?

android - 函数的返回类型为 'Future<Station>',但没有以return语句flutter结尾

Flutter 聊天文本像 Whatsapp 或 Telegram 一样对齐

flutter - 如何在 flutter 测试中模拟 onDoubleTap