运行依赖于 SharedPreferences 插件的测试总是会导致
MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences)
我的 pubspec.yaml
dev_dependencies:
flutter_test:
sdk: flutter
dependencies:
flutter:
sdk: flutter
shared_preferences: 0.2.3
的代码在应用程序本身中运行良好。 为了运行使用插件的测试,我是否遗漏了一些我需要做的事情?
最佳答案
如果您使用的是 shared_preferences 0.2.4 及更高版本,请使用 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;
});
关于dart - Flutter 测试 MissingPluginException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44357053/