flutter - Firebase.initializeApp() works in main() but fails in setUpAll() with PlatformException(channel-error) during unit tes

I am trying to run unit tests for my Flutter project that uses Firebase Firestore, and I want to test i

I am trying to run unit tests for my Flutter project that uses Firebase Firestore, and I want to test it with the Firebase Emulator. I am able to initialize Firebase and connect to the Firestore Emulator successfully when running the app normally in the main() function, but I get an error when trying to initialize Firebase within the setUpAll() function in my test suite.

This code works as expected when running the app normally:

void main() async {
 WidgetsFlutterBinding.ensureInitialized();
 await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
 runApp(const MyApp());
}

When I try to initialize Firebase within setUpAll() in my test file, I get the following error:

PlatformException(channel-error, Unable to establish connection on channel., null, null) package:firebase_core_platform_interface/src/pigeon/messages.pigeon.dart 210:7 FirebaseCoreHostApi.initializeCore

Here is my test file:

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:task_manager/data/model/user_model.dart';
import 'package:task_manager/data/repository/user_repository/firebase_user_repository.dart';
import 'package:task_manager/firebase_options.dart';

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

  late FirebaseFirestore firestore;
  late FirebaseUserRepository userRepository;

  setUpAll(() async {
    await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
    FirebaseFirestore.instance.useFirestoreEmulator('localhost', 8080);
  });

  setUp(() async {
    userRepository = FirebaseUserRepository(userCollectionRef: FirebaseFirestore.instance.collection('users'));

    await FirebaseFirestore.instance.terminate();
    await FirebaseFirestore.instance.clearPersistence();
  });

  group('test addUser', () {
    test('Should add a user and retrieve it from Firestore', () async {
      final user = UserModel(id: '1', name: 'bob', email: '[email protected]');
      final userMap = user.toMap();
      await userRepository.addUser(user.id, userMap);
    });
  });
}

I am trying to run unit tests for my Flutter project that uses Firebase Firestore, and I want to test it with the Firebase Emulator. I am able to initialize Firebase and connect to the Firestore Emulator successfully when running the app normally in the main() function, but I get an error when trying to initialize Firebase within the setUpAll() function in my test suite.

This code works as expected when running the app normally:

void main() async {
 WidgetsFlutterBinding.ensureInitialized();
 await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
 runApp(const MyApp());
}

When I try to initialize Firebase within setUpAll() in my test file, I get the following error:

PlatformException(channel-error, Unable to establish connection on channel., null, null) package:firebase_core_platform_interface/src/pigeon/messages.pigeon.dart 210:7 FirebaseCoreHostApi.initializeCore

Here is my test file:

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:task_manager/data/model/user_model.dart';
import 'package:task_manager/data/repository/user_repository/firebase_user_repository.dart';
import 'package:task_manager/firebase_options.dart';

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

  late FirebaseFirestore firestore;
  late FirebaseUserRepository userRepository;

  setUpAll(() async {
    await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
    FirebaseFirestore.instance.useFirestoreEmulator('localhost', 8080);
  });

  setUp(() async {
    userRepository = FirebaseUserRepository(userCollectionRef: FirebaseFirestore.instance.collection('users'));

    await FirebaseFirestore.instance.terminate();
    await FirebaseFirestore.instance.clearPersistence();
  });

  group('test addUser', () {
    test('Should add a user and retrieve it from Firestore', () async {
      final user = UserModel(id: '1', name: 'bob', email: '[email protected]');
      final userMap = user.toMap();
      await userRepository.addUser(user.id, userMap);
    });
  });
}

Share Improve this question asked Mar 7 at 8:59 TONI IVANOVTONI IVANOV 634 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

I managed to find the solution. There are 2 things that need to be done:

  1. Start the android/ios emulator.

  2. Move the testing file into /integration_test folder.

After that you will be able to interact with the Firebase emulator.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744940389a4602286.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信