flutter 在通道上找不到方法读取的实现plugins.it,

wtlkbnrh  于 2022-12-14  发布在  Flutter
关注(0)|答案(3)|浏览(183)

我正在尝试为ApiBaseHelper方法(负责所有HTTP请求)编写一些单元测试。

class ApiBaseHelper {
  String _baseUrl = "${Config.get('apiUrl')}/";
  Dio _dio = Dio()..interceptors.add(DioCustomInterceptors());

  ApiBaseHelper({Dio? dio}) {
    _dio = dio ?? Dio()
      ..interceptors.add(DioCustomInterceptors());
    _dio.options.baseUrl = _baseUrl;
  }

  Future<dynamic> get(String url, [Map<String, dynamic>? query]) async {
    try {
      Response<String> response =
          await _dio.get(_baseUrl + url, queryParameters: query);
      if (response.statusCode == 200) {
        return response.data;
      }
    } on DioError catch (e) {
      _returnException(e.response);
    }
  }
}

这是我尝试测试GET方法的文件,我使用的是Dio包

import 'package:app/utils/services/network/api_base_helper.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http_mock_adapter/http_mock_adapter.dart';

const successMessage = {'message': 'Success'};
const errorMessage = {'message': 'error'};
const testPath = 'test';
const testData = {'data': 'sample data'};
const header = {'Content-Type': 'application/json'};

void main(){
  WidgetsFlutterBinding.ensureInitialized() ;
  final dio = Dio();
  final dioAdapter = DioAdapter(dio: dio);
  var baseUrl;

  setUp(() {
    dio.httpClientAdapter = dioAdapter;
    baseUrl = 'https://example.com/';
  });

  group("", (){
    test('Test Get', () async {
      dioAdapter.onGet(
        '$baseUrl$testPath',
            (request) {
          return request.reply(200, successMessage);
        },
        data: null,
        queryParameters: {},
        headers: {},
      );
      final service = ApiBaseHelper(
        dio: dio,
      );
      final response = await service.get('test');
      expect(response, successMessage);
    });
  });
}

当我运行测试时,我得到这个错误

package:flutter/src/services/platform_channel.dart 175:7       MethodChannel._invokeMethod
===== asynchronous gap ===========================
dart:async                                                     _asyncErrorWrapperHelper
package:dio/src/dio_mixin.dart 502:28                          DioMixin.fetch._requestInterceptorWrapper.<fn>.<fn>.<fn>
package:dio/src/dio_mixin.dart 818:22                          DioMixin.checkIfNeedEnqueue
package:dio/src/dio_mixin.dart 500:22                          DioMixin.fetch._requestInterceptorWrapper.<fn>.<fn>
===== asynchronous gap ===========================
dart:async                                                     new Future
package:dio/src/dio_mixin.dart 499:13                          DioMixin.fetch._requestInterceptorWrapper.<fn>
package:dio/src/dio_mixin.dart 494:14                          DioMixin.fetch._requestInterceptorWrapper.<fn>
===== asynchronous gap ===========================
dart:async                                                     Future.then
package:dio/src/dio_mixin.dart 581:23                          DioMixin.fetch.<fn>
dart:collection                                                ListMixin.forEach
package:dio/src/dio_mixin.dart 577:18                          DioMixin.fetch
package:dio/src/dio_mixin.dart 468:12                          DioMixin.request
package:dio/src/dio_mixin.dart 55:12                           DioMixin.get
package:app/utils/services/network/api_base_helper.dart 19:22  ApiBaseHelper.get
test/network/api_base_helper_test.dart 40:38                   main.<fn>.<fn>
test/network/api_base_helper_test.dart 27:22                   main.<fn>.<fn>

MissingPluginException(No implementation found for method read on channel plugins.it_nomads.com/flutter_secure_storage)

我正在使用Flutter安全存储来存储用户凭据,但在这里我只是尝试测试ApiBaseHelper。需要帮助吗?

g6ll5ycj

g6ll5ycj1#

停止应用程序调试并重新启动。这必须解决issue

6uxekuva

6uxekuva2#

当我的integration_test位于/test/integration_test下时,我也遇到了同样的错误
尝试将测试文件夹代码直接放在项目根文件夹中,例如/test或/integration_test(与/lib处于同一级别)

g0czyy6m

g0czyy6m3#

当我尝试测试Flutter_Secure_Storage提供的另一种方法时,我遇到了完全相同的错误。
我可以通过在setUpAll函数中编写以下代码来修复此问题:

setUpAll(() {
  FlutterSecureStorage.setMockInitialValues({});
});

更多信息,请访问:Flutter Test MissingPluginException
希望这对你有帮助。

相关问题