简体   繁体   English

MissingPluginException(未找到在通道插件上读取的方法的实现。it_nomads.com/flutter_secure_storage)

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

I'm trying to write some unit tests for ApiBaseHelper methods (responsible for all HTTP requests).我正在尝试为 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);
    }
  }
}

this is the file where I tried to test the GET method, I use the Dio package这是我尝试测试 GET 方法的文件,我使用 Dio package

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);
    });
  });
}

when I run test I got this erro当我运行测试时,我得到了这个错误

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)

I'm using flutter secure storage to store user credential, but here I'm just trying to test ApiBaseHelper.我正在使用 flutter 安全存储来存储用户凭据,但在这里我只是想测试 ApiBaseHelper。 any help please?有什么帮助吗?

Stop the application debug and relaunch it.停止应用程序调试并重新启动它。 This must resolve the issue .这必须解决问题

Check your test code folder is directly /integration_test folder.检查你的测试代码文件夹是直接/integration_test 文件夹。 (at same level as /lib) (与 /lib 处于同一级别)

I had the same behaviour when my integration_test was under /test/integration_test当我的 integration_test 在 /test/integration_test 下时,我有同样的行为

I had the exact same error when I was trying to test a different method provided by Flutter_Secure_Storage.当我尝试测试 Flutter_Secure_Storage 提供的不同方法时,我遇到了完全相同的错误。

I was able to fix the issue by writing the following in setUpAll function:我能够通过在 setUpAll function 中编写以下内容来解决问题:

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

More info here: Flutter Test MissingPluginException此处提供更多信息: Flutter 测试 MissingPluginException

Hope this helps.希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未找到在通道插件上写入方法的实现。it_nomads.com/flutter_secure_storage - No implementation found for method write on channel plugins.it_nomads.com/flutter_secure_storage MissingPluginException(MissingPluginException(在通道 plugins.flutter.io/firebase_core 上找不到方法 Firebase#initializeCore 的实现) - MissingPluginException (MissingPluginException(No implementation found for method Firebase#initializeCore on channel plugins.flutter.io/firebase_core) MissingPluginException(在通道 flutter 上未找到方法 requestPermissions 的实现 - MissingPluginException(No implementation found for method requestPermissions on channel flutter Flutter:错误:MissingPluginException(在通道插件上找不到方法 getTemporaryDirectory 的实现。flutter.io/path_provider) - Flutter: Error: MissingPluginException(No implementation found for method getTemporaryDirectory on channel plugins.flutter.io/path_provider) 未处理的异常:MissingPluginException(对于 Flutter 上的许多包,通道插件上的方法 getAll 未找到实现。flutter.io/ - Unhandled Exception: MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/ for many packages on Flutter Flutter 测试:MissingPluginException(在通道 plugins.flutter.io/shared_preferences 上找不到方法 getAll 的实现) - Flutter Test: MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences) Flutter - MissingPluginException(在通道 plugins.flutter.io/image_picker 上找不到方法 pickImage 的实现) - Flutter - MissingPluginException (No implementation found for method pickImage on channel plugins.flutter.io/image_picker) Flutter:错误:MissingPluginException(在通道插件上找不到方法 getAll 的实现。flutter.io/package_info) - Flutter: Error: MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/package_info) Flutter:MissingPluginException(未在通道插件上找到方法 createUserWithEmailAndPassword 的实现。flutter.io/firebase_auth) - Flutter: MissingPluginException(No implementation found for method createUserWithEmailAndPassword on channel plugins.flutter.io/firebase_auth) 未处理的异常:MissingPluginException(在通道 plugins.flutter.io/image_picker_android 上找不到方法 pickImage 的实现) - Unhandled Exception: MissingPluginException(No implementation found for method pickImage on channel plugins.flutter.io/image_picker_android)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM