Running tests which rely on the SharedPreferences Plugin always result in
MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences)
My pubspec.yaml
dev_dependencies: flutter_test: sdk: flutter dependencies: flutter: sdk: flutter shared_preferences: 0.2.3
The code for works fine in the application itself. Am i missing something i need to do in order to run tests which make use of a plugin?
There are three types of tests that Flutter supports. A unit test verifies the behavior of a method or class. A widget test verifies the behavior of Flutter widgets without running the app itself. An integration test (also called end-to-end testing or GUI testing) runs the full app.
Integration tests in Flutter are written using the integration test package, provided by the SDK. This is Flutter's version of Selenium WebDriver (generic web), Protractor (Angular), Espresso (Android), or Earl Gray (iOS). The package internally uses flutter driver to drive the test on a device.
If you're using shared_preferences 0.2.4 and above, use setMockInitialValues
:
SharedPreferences.setMockInitialValues({}); // set initial values here if desired
For earlier versions you can do it manually:
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; });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With