0

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?

Jaimin Modi
  • 1,530
  • 4
  • 20
  • 72
  • By looking at the error: `No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()` I think the issue is not with the firebase not able to initialise the app but it’s not able to figure out your registered package name for the given platform. As you mentioned that you're testing this against web (my main flutter web page), make sure that firebase is aware about your project domain or at least has context to it. Right now since firebase found nothing, it’s taking 'DEFAULT' package. – Jeel Vankhede Jul 19 '23 at 15:14
  • But actually, the web app is running quite okay and i am also able to get and post data on firestore database, firebase authentication is also works fine. The issue is while I try to execute my simple test case for the screen. – Jaimin Modi Jul 21 '23 at 06:11

1 Answers1

0
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();  

Adil Shinwari
  • 408
  • 2
  • 10