2

I am new to writing widget test cases in Flutter and I found very less resources on Flutter testing, When I am trying to write test cases for a module which uses Firestore and it gives following error: No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp() - Error 1

MapsRepository of MapsBloc is using Firestore to get data

I tried so many examples and the following code is working but giving another error: FirebaseCoreHostApi.initializeCore PlatformException(channel-error, Unable to establish connection on channel., null, null) - Error 1 is not observed here

Test file

void main() async{
  TestWidgetsFlutterBinding.ensureInitialized();
  //WidgetsFlutterBinding.ensureInitialized();

  setUpAll(() async {
    WidgetsFlutterBinding.ensureInitialized();
    await Firebase.initializeApp(
      options: DefaultFirebaseOptions.currentPlatform,
    ); // setupall method is eliminated Error 1

  });
  
  testWidgets('Form Widget Test', (tester) async {

    await tester.pumpWidget(initApp());
    await tester.pumpAndSettle();
    await tester.pump(const Duration(seconds: 2));

    // Create the Finders.
    final stack = find.byType(Stack);

    // Create the Matchers
    expect(stack, findsWidgets);

  });
}
Widget initApp() {

  MapsRepository mapsRepository = MapsRepository();
  MapsBloc mapsBloc = MapsBloc();
  MyMaps myMaps = const MyMaps();

  return EasyLocalization(
    supportedLocales: const [Locale('en', 'US'), Locale('hi', 'IN')],
    path: 'assets/translations',
    fallbackLocale: const Locale('en', 'US'),
    child: MultiProvider(
      providers: [
        BlocProvider<MapsBloc>.value(
          value: mapsBloc,
          child: myMaps,
        ),
      ],
      child: const MyApp(),
    ),
  );
}

What I am missing here, same structure working for other test files. Some test files are passing test cases some are throwing error

I tried printing like below

await Firebase.initializeApp(
      options: DefaultFirebaseOptions.currentPlatform,
    ).whenComplete(() => print("completed init"));

This is also not working. Any working examples would be really helpful. Thank you

Padmaja Rani
  • 113
  • 3
  • 10

0 Answers0