Flutter Test MissingPluginException

时间:2017-06-04 17:45:27

标签: dart flutter

运行依赖于SharedPreferences插件的测试总是会导致

class Url < ActiveRecord::Base
  validates :url, :presence => true
end

我的pubspec.yaml

MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences)

代码在应用程序本身中正常工作。 我是否缺少一些我需要做的事情才能运行使用插件的测试?

3 个答案:

答案 0 :(得分:14)

如果您使用的是0.2.4及更高版本的shared_preferences,请使用setMockInitialValues

SharedPreferences.setMockInitialValues({}); // set initial values here if desired

对于早期版本,您可以手动执行:

const MethodChannel('plugins.flutter.io/shared_preferences')
  .setMockMethodCallHandler((MethodCall methodCall) async {
    if (methodCall.method == 'getAll') {
      return <String, dynamic>{}; // set initial values here if desired
    }
    return null;
  });

答案 1 :(得分:3)

@Siman谢谢

版本shared_preferences: ^0.5.12

ad SharedPreferences.setMockInitialValues({}); 在Flutter App主要功能中的runApp()函数之前

为我修复此错误

enter image description here

答案 2 :(得分:2)

我对flutter_secure_storage插件有完全相同的问题。我认为问题在于这两个插件都依赖于手机或仿真器(而不是应用程序中的内容)上的存储,因此在测试环境中不可用。尝试通过执行flutter run your_test_file.dart直接运行测试。根据{{​​3}},这应该“在首选的运行时环境(例如模拟器或设备)中执行测试”。对我来说效果很好。