7

I am using Flutter and Firebase as my database. I want to do some unit test of my application, but when I start this test :

testWidgets('MyWidget has a title and message', (WidgetTester tester) async {
    // Create the widget by telling the tester to build it.
    await tester.pumpWidget(LoginPage());

    expect(true, true);
});

I have the Exception :

No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()

But Firebase.initializeApp() is called in my main.dart here :

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

And I don't know how to initialize the Firebase app in my test.

1

1 Answer 1

4

Posting this as a community wiki as it's based on @GuilhermeGabanelli's comment.

If you check this flutterfire documentation on how to perform unit tests with Firebase Services you will see that:

The Firebase libraries need to run on an actual device or emulator. So if you want to run unit tests, you'll have to use Fakes instead. A Fake is a library that implements the API of a given Firebase library and simulates its behavior.

...

When initializing your app, instead of passing the actual instance of a Firebase library (e.g. FirebaseFirestore.instance if using Firestore), you pass an instance of a fake (e.g. FakeFirebaseFirestore()). Then the rest of your application will run as if it were talking to Firebase.

Followed by an example with sample code. I believe this is the cause of the issue you are facing and if you change your code to use Fakes this should be fixed.

Sign up to request clarification or add additional context in comments.

1 Comment

This link doesn't seem to work, any updates?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.