Jaimin Modi
Jaimin Modi

Reputation: 1677

Flutter Test Cases issue : EXCEPTION CAUGHT BY WIDGETS LIBRARY - 'No Firebase App has been created'

I am doing widget testing and for my main flutter web page I have written a simple test case as below:

  void main(){
  group("test cases for main screen page after login",(){
    testWidgets("should have one title for the page on top", (widgetTester) async {
      //Arrange
      await widgetTester.pumpWidget(const MaterialApp(home:MainScreenPage()));
      //Act
      Finder titleBookTracker = find.byKey(const ValueKey("text_title_main"));
      //Assert
      expect(titleBookTracker, findsOneWidget);
    });
  });
}

Here, In my main page screen, I am using Firebase stuffs to load and post the data to Fire-store database.

The Issue is while I am running the above test case, It giving me below exception:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ The following FirebaseException was thrown building MainScreenPage(dirty): [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()

The relevant error-causing widget was: MainScreenPage
MainScreenPage:file:///home/StudioProjects/book_repository_sample/test/main_screen_page_test.dart:9:60

As you can see above the issue it showing me at line number 9 which is:

import 'package:cloud_firestore/cloud_firestore.dart';

What might be the issue?

Upvotes: 1

Views: 598

Answers (1)

Adil Shinwari
Adil Shinwari

Reputation: 796

void main()async{
  WidgetsFlutterBinding.ensureInitialized(); // add this line
  await Firebase.initializeApp(); //add this line
  group("test cases for main screen page after login",(){
    testWidgets("should have one title for the page on top", (widgetTester) async {
      //Arrange
      await widgetTester.pumpWidget(const MaterialApp(home:MainScreenPage()));
      //Act
      Finder titleBookTracker = find.byKey(const ValueKey("text_title_main"));
      //Assert
      expect(titleBookTracker, findsOneWidget);
    });
  });
}

You have to add googleServices.json in android/app/build.gradle. and you have to add these two line in main function and make sure to make main function async

WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();  

Upvotes: -1

Related Questions