2

Used SharedPreference throwing error in Unit test. In one function I have used SharedPreference and during it's unit testing getting error as "Binding has not yet been initialized" and many other errors too.

Working unit test: enter image description here

After uncommenting SharedPreference code: enter image description here

Below is the code of my User.dart file

import 'package:shared_preferences/shared_preferences.dart';

class User {
  final String name;

  User(this.name);
  Future<bool> userName() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setString("name", name);
    return true;
  }
}

and below is the code of test file (User_test.dart)

import 'dart:math';

import 'package:flutter_test/flutter_test.dart';
import 'package:unit_testf/user.dart';

void main() {
  test("a unit test", () async {
    User user = User("flutter");
    final result = await user.userName();
    expect(result, true);
  });
}

Below is the errors I am getting.

Binding has not yet been initialized.
The "instance" getter on the ServicesBinding binding mixin is only available once that binding has been initialized.
Typically, this is done by calling "WidgetsFlutterBinding.ensureInitialized()" or "runApp()" (the latter calls the former). Typically this call is done in the "void main()" method. The "ensureInitialized" method is idempotent; calling it multiple times is not harmful. After calling that method, the "instance" getter will return the binding.
In a test, one can call "TestWidgetsFlutterBinding.ensureInitialized()" as the first line in the test's "main()" method to initialize the binding.
If ServicesBinding is a custom binding mixin, there must also be a custom binding class, like WidgetsFlutterBinding, but that mixes in the selected binding, and that is the class that must be constructed before using the "instance" getter.
package:flutter/src/foundation/binding.dart 284:9                               BindingBase.checkInstance.<fn>
package:flutter/src/foundation/binding.dart 366:6                               BindingBase.checkInstance
package:flutter/src/services/binding.dart 54:54                                 ServicesBinding.instance
package:flutter/src/services/platform_channel.dart 166:45                       BasicMessageChannel.binaryMessenger
package:flutter/src/services/platform_channel.dart 180:38                       BasicMessageChannel.send
package:shared_preferences_foundation/messages.g.dart 115:52                    UserDefaultsApi.getAll
package:shared_preferences_foundation/shared_preferences_foundation.dart 46:53  SharedPreferencesFoundation.getAll
package:shared_preferences/shared_preferences.dart 164:57                       SharedPreferences._getSharedPreferencesMap
package:shared_preferences/shared_preferences.dart 33:19                        SharedPreferences.getInstance
package:unit_testf/user.dart 8:55                                               User.userName
test/user_test.dart 9:31                                                        main.<fn>

Below is the output of my flutter doctor command

Last login: Thu Feb 16 09:58:26 on console
anandsuthar@anands-MacBook-Air ~ % flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.3.10, on macOS 13.1 22C65 darwin-x64, locale en-GB)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.1)
[✓] VS Code (version 1.75.1)
[✓] Connected device (2 available)
[✓] HTTP Host Availability

• No issues found!
anandsuthar@anands-MacBook-Air ~ % 
Anand Suthar
  • 3,678
  • 2
  • 30
  • 52

2 Answers2

4

Based on context you are running you have to be sure that ServicesBinding has been initialized.

Making something which start with showing an Widget as the first thing is easily call the rest as ServicesBinding has been initialized. But calling method/classes that have to been initialized yet you must be sure of that initialized by calling those two methods which explicitly invoking the initialize.

TestWidgetsFlutterBinding.ensureInitialized();

Or

WidgetsFlutterBinding.ensureInitialized();

So your main method have to be:

void main() async {
  // First call
  WidgetsFlutterBinding.ensureInitialized();

  // Then Your code after
  //……
  //……
  //…...
}
G3nt_M3caj
  • 2,497
  • 1
  • 14
  • 16
0

It seems that you're trying to invoke the .getInstance() method on shared preferences, which is not available during a unit test as you implicitly try to invoke its behavior. Since you only try to 'unit' test your User class, you want to mock the Shared Preferences anyway since you're not testing the functionality of that. It is good practice to separate the classes you test, and treat everything the class uses internally as a black-box solution, which means you will need to mock it.

Try looking into the solution mentioned here to see how to mock the Shared Preferences behavior.

The error message is coming from the fact that you're trying to invoke a method on a class that is only available during the actual running of a Flutter app. Since your unit tests do not create this complete environment (i.e. the app is not built completely for each test), it is saying this class is not instantiated. The solution of providing the WidgetsFlutterBinding is thus not going to help, as you instantiate no widgets in your test.

Let me know if it works!