Flutterfire: Running Test throws MissingPluginException(No implementation found for method Firebase#initializeCore on channel plugins.flutter.io/firebase_core

Created on 24 Aug 2020  ยท  22Comments  ยท  Source: FirebaseExtended/flutterfire

Failing to write simple unit test using firebase emulators
Yesterday I upgrade to latest and greatest firebase/firestore plugins and decided to try out the firebase emulators to add some testing of my data access objects.

I fail to get the test up and running agains the emulators due to the error below

I saw some other issues that has been closed regarding the same error but with no solution that suited me.

I guess this is a simple mistake form my side in the code when connecting to the emulators.

_MissingPluginException(No implementation found for method Firebase#initializeCore on channel plugins.flutter.io/firebase_core)_

My test code

/// TODO: Get firebase emulators to work an use them for unit testing dao's
void main() {

  TournamentDao dao;

  setUpAll(() async {
    WidgetsFlutterBinding.ensureInitialized();

    await Firebase.initializeApp();
    FirebaseFirestore.instance.settings = Settings(host: 'localhost:8080', sslEnabled: false, persistenceEnabled: false);

    GetIt.I.registerSingleton<TournamentDao>(TournamentDaoImpl());
  });

  setUp(() {
    dao = GetIt.I<TournamentDao>();
  });

  test("TournamentDao.crud", () async {

    Tournament t = Tournament();
    t.name = 'Tezt';
    dao.create(t);

    var list = await dao.list(10);
    expect(list.length, 1);
  });

Firebase versions

  cloud_firestore: ^0.14.0
  firebase_auth: 0.18.0
  firebase_core: ^0.5.0
  firebase: ^7.3.0

Firebase emulators startup

firebase emulators:start
i  emulators: Starting emulators: functions, firestore, database, hosting
โš   functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: pubsub
โš   Your requested "node" version "10" doesn't match your global version "12"
i  firestore: Firestore Emulator logging to firestore-debug.log
i  database: Database Emulator logging to database-debug.log
i  hosting: Serving hosting files from: public
โœ”  hosting: Local server: http://localhost:5000
i  ui: Emulator UI logging to ui-debug.log
i  functions: Watching "/Users/gunnar/git/dart/chess-champion/functions" for Cloud Functions...

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ โœ”  All emulators ready! View status and logs at http://localhost:4000 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Emulator  โ”‚ Host:Port      โ”‚ View in Emulator UI             โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Functions โ”‚ localhost:5001 โ”‚ http://localhost:4000/functions โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Firestore โ”‚ localhost:8080 โ”‚ http://localhost:4000/firestore โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Database  โ”‚ localhost:9000 โ”‚ http://localhost:4000/database  โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Hosting   โ”‚ localhost:5000 โ”‚ n/a                             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  Other reserved ports: 4400, 4500

Issues? Report them at https://github.com/firebase/firebase-tools/issues and attach the *-debug.log files.

Flutter doctor
Run flutter doctor and paste the output below:

Doctor summary (to see all details, run flutter doctor -v):
[โœ“] Flutter (Channel stable, 1.20.2, on Mac OS X 10.15.6 19G73, locale sv-SE)

[โœ“] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[โœ“] Xcode - develop for iOS and macOS (Xcode 11.6)
[โœ“] Android Studio (version 4.0)
[โœ“] IntelliJ IDEA Ultimate Edition (version 2020.2)
[โœ“] Connected device (1 available)

โ€ข No issues found!
core bug

Most helpful comment

I am also facing this issue. Everything runs fine except in testing.

All 22 comments

The error "MissingPluginException(No implementation found for method Firebase#initializeCore on channel plugins.flutter.io/firebase_core)" points towards your Dart code being updated, but your native code hasn't updated.

Can you try running flutter clean and rebuilding?

I have already tried that!

mรฅn 24 aug. 2020 kl. 11:43 skrev Elliot Hesp notifications@github.com:

>
>

The error "MissingPluginException(No implementation found for method
Firebase#initializeCore on channel plugins.flutter.io/firebase_core)"
points towards your Dart code being updated, but your native code hasn't
updated.

Can you try running flutter clean and rebuilding?

โ€”
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/FirebaseExtended/flutterfire/issues/3311#issuecomment-679025147,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AACFWMVS2WDEP5PY6BELKITSCIY4HANCNFSM4QJJEAGQ
.

--
Mobil: 0708-52 62 90
Teknologgatan 3, 113 60 Stockholm
http://www.oakstair.se

I saw some comment about using localhost but have tried 0.0.0.0 or my ip with same result.

The emulator vs the error message have no relevance - try wiping all the caches, uninstalling the app etc, something is being cached.

Ok I get this in my development environment running unit tests.

flutter clean does not work

What do you mean by wiping all caches ?
How do I do that?

Den mรฅn 24 aug. 2020 kl 14:40 skrev Elliot Hesp notifications@github.com:

The emulator vs the error message have no relevance - try wiping all the
caches, uninstalling the app etc, something is being cached.

โ€”
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/FirebaseExtended/flutterfire/issues/3311#issuecomment-679101974,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AACFWMXV3SY6M4PDW42PR6DSCJNSRANCNFSM4QJJEAGQ
.

--
Mobil: 0708-52 62 90
Teknologgatan 3, 113 60 Stockholm
http://www.oakstair.se

Even if there is no relevance it is thrown when calling Firebase.initializeApp

Skaฬˆrmavbild 2020-08-24 kl  18 17 44

Ok I get it now that this has nothing to do with pointing firebase to the emulator. I get the same error also when removing the line that configs towards the emulator.

It must be something that differs when running my tests and when running my flutter app that causes this.

My full test class attempt looks like this.

import 'package:chess_champion/model/dao/tournament_dao.dart';
import 'package:chess_champion/model/entity/tournament.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/cupertino.dart';
import 'package:get_it/get_it.dart';
import 'package:test/test.dart';

/// TODO: Get firebase emulators to work and use them for unit testing dao's
void main() {

  TournamentDao dao;

  setUpAll(() async {
    WidgetsFlutterBinding.ensureInitialized();

    await Firebase.initializeApp();
    // 192.168.0.31
    // FirebaseFirestore.instance.settings = Settings(host: '0.0.0.0:8080', sslEnabled: false, persistenceEnabled: true);

    GetIt.I.registerSingleton<TournamentDao>(TournamentDaoImpl());
  });

  setUp(() {
    dao = GetIt.I<TournamentDao>();
  });

  test("TournamentDao.crud", () async {
    Tournament t = Tournament();
    t.name = 'Tezt';
    dao.create(t);

    var list = await dao.list(10);
    expect(list.length, 1);
  });

}


I have the same issue. It works fine when I initialize the app in my actual app, but the same MissingPluginException gets thrown when I attempt the same thing in one of my simple tests.

Hi @oakstair
Can you please provide a flutter test --verbose logs, and a minimal complete reproducible code sample without using third party packages
Thank you


Hey @oakstair. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 7 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

I will try to get some time today to do a mini project that reproduces this.

@eriklange You donโ€™t have a mini project?


logs

[ +242 ms] test 0: starting test
/Users/tahatesser/AndroidStudioProjects/triage/flutterfire_examples/firebase_auth_example/test/user_dao_test.dart
[  +16 ms] test 0: starting shell process
[   +8 ms] Stopping scan for flutter_test_config.dart; found project root at
/Users/tahatesser/AndroidStudioProjects/triage/flutterfire_examples/firebase_auth_example
[   +6 ms] Compiler will use the following file as its incremental dill file:
/var/folders/7t/qr_jg4155x53wn0m3nmqb8g80000gn/T/flutter_tools.pziWSp/flutter_test_compiler.eZLb10/output.dill
[        ] Listening to compiler controller...
[   +7 ms] Compiling
file:///var/folders/7t/qr_jg4155x53wn0m3nmqb8g80000gn/T/flutter_tools.pziWSp/flutter_test_listener.NCG87j/listener.dart
[  +12 ms] /Users/tahatesser/Code/flutter_dev/bin/cache/dart-sdk/bin/dart --disable-dart-dev
/Users/tahatesser/Code/flutter_dev/bin/cache/artifacts/engine/darwin-x64/frontend_server.dart.snapshot --sdk-root
/Users/tahatesser/Code/flutter_dev/bin/cache/artifacts/engine/common/flutter_patched_sdk/ --incremental --target=flutter
--debugger-module-names --experimental-emit-debug-metadata -Ddart.developer.causal_async_stacks=true --output-dill
/var/folders/7t/qr_jg4155x53wn0m3nmqb8g80000gn/T/flutter_tools.pziWSp/flutter_test_compiler.eZLb10/output.dill --packages
/Users/tahatesser/AndroidStudioProjects/triage/flutterfire_examples/firebase_auth_example/.packages -Ddart.vm.profile=false
-Ddart.vm.product=false
--bytecode-options=source-positions,local-var-info,debugger-stops,instance-field-initializers,keep-unreachable-code,avoid-cl
osure-call-instructions --enable-asserts --track-widget-creation --initialize-from-dill
/Users/tahatesser/AndroidStudioProjects/triage/flutterfire_examples/firebase_auth_example/build/testfile.dill.track.dill
[  +30 ms] <- compile
file:///var/folders/7t/qr_jg4155x53wn0m3nmqb8g80000gn/T/flutter_tools.pziWSp/flutter_test_listener.NCG87j/listener.dart
[+1077 ms] <- accept
[        ] <- reset
[        ] Compiling
/var/folders/7t/qr_jg4155x53wn0m3nmqb8g80000gn/T/flutter_tools.pziWSp/flutter_test_listener.NCG87j/listener.dart took 1120ms
[   +1 ms] /Users/tahatesser/Code/flutter_dev/bin/cache/artifacts/engine/darwin-x64/flutter_tester --disable-observatory
--enable-checked-mode --verify-entry-points --enable-software-rendering --skia-deterministic-rendering
--enable-dart-profiling --non-interactive --use-test-fonts
--packages=/Users/tahatesser/AndroidStudioProjects/triage/flutterfire_examples/firebase_auth_example/.packages
/var/folders/7t/qr_jg4155x53wn0m3nmqb8g80000gn/T/flutter_tools.pziWSp/flutter_test_listener.NCG87j/listener.dart.dill
[   +1 ms] Using this directory for fonts configuration:
/var/folders/7t/qr_jg4155x53wn0m3nmqb8g80000gn/T/flutter_tools.pziWSp/flutter_test_fonts.0NNQl7
[   +7 ms] test 0: awaiting initial result for pid 14109
[ +336 ms] test 0: process with pid 14109 connected to test harness
[   +1 ms] test 0: awaiting test result for pid 14109
00:01 +0 -2: /Users/tahatesser/AndroidStudioProjects/triage/flutterfire_examples/firebase_auth_example/test/user_dao_test.dart: (setUpAll) [E]
  MissingPluginException(No implementation found for method Firebase#initializeCore on channel plugins.flutter.io/firebase_core)
  package:flutter/src/services/platform_channel.dart 157:7  MethodChannel._invokeMethod

00:01 +0 -2: ... (tearDownAll)                                                                                             [ +225 ms] test 0: process with pid 14109 no longer needed by test harness
[        ] test 0: cleaning up...
[        ] test 0: ensuring end-of-process for shell
[  +10 ms] test 0: deleting temporary directory
[   +3 ms] test 0: shutting down test harness socket server
[   +2 ms] test 0: finished
00:01 +0 -2: Some tests failed.                                                                                            
[  +11 ms] Deleting /var/folders/7t/qr_jg4155x53wn0m3nmqb8g80000gn/T/flutter_tools.pziWSp/flutter_test_compiler.eZLb10...
[   +3 ms] killing pid 14101
[  +35 ms] Deleting /var/folders/7t/qr_jg4155x53wn0m3nmqb8g80000gn/T/flutter_tools.pziWSp/flutter_test_fonts.0NNQl7...
[   +5 ms] test package returned with exit code 1
[  +10 ms] "flutter test" took 3,057ms.
[   +6 ms] 
           #0      throwToolExit (package:flutter_tools/src/base/common.dart:14:3)
           #1      TestCommand.runCommand (package:flutter_tools/src/commands/test.dart:282:7)
           <asynchronous suspension>
           #2      FlutterCommand.verifyThenRunCommand (package:flutter_tools/src/runner/flutter_command.dart:912:18)
           #3      _rootRunUnary (dart:async/zone.dart:1198:47)
           #4      _CustomZone.runUnary (dart:async/zone.dart:1100:19)
           #5      _FutureListener.handleValue (dart:async/future_impl.dart:143:18)
           #6      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:696:45)
           #7      Future._propagateToListeners (dart:async/future_impl.dart:725:32)
           #8      Future._completeWithValue (dart:async/future_impl.dart:529:5)
           #9      Future._asyncCompleteWithValue.<anonymous closure> (dart:async/future_impl.dart:567:7)
           #10     _rootRun (dart:async/zone.dart:1190:13)
           #11     _CustomZone.run (dart:async/zone.dart:1093:19)
           #12     _CustomZone.runGuarded (dart:async/zone.dart:997:7)
           #13     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
           #14     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
           #15     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
           #16     _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:118:13)
           #17     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:169:5)


[ +164 ms] ensureAnalyticsSent: 160ms
[   +2 ms] Running shutdown hooks
[        ] Shutdown hook priority 4
[   +1 ms] Shutdown hooks complete
[        ] exiting with code 1


flutter doctor -v

[โœ“] Flutter (Channel dev, 1.22.0-9.0.pre, on Mac OS X 10.15.6 19G2021, locale
    en-GB)
    โ€ข Flutter version 1.22.0-9.0.pre at /Users/tahatesser/Code/flutter_dev
    โ€ข Framework revision 7a43175198 (6 days ago), 2020-08-28 23:18:04 -0400
    โ€ข Engine revision 07e2520d5d
    โ€ข Dart version 2.10.0 (build 2.10.0-73.0.dev)


[โœ“] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    โ€ข Android SDK at /Users/tahatesser/Code/sdk
    โ€ข Platform android-29, build-tools 29.0.2
    โ€ข ANDROID_HOME = /Users/tahatesser/Code/sdk
    โ€ข Java binary at: /Applications/Android
      Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    โ€ข Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)
    โ€ข All Android licenses accepted.

[โœ“] Xcode - develop for iOS and macOS (Xcode 11.7)
    โ€ข Xcode at /Applications/Xcode.app/Contents/Developer
    โ€ข Xcode 11.7, Build version 11E801a
    โ€ข CocoaPods version 1.9.3

[โœ“] Chrome - develop for the web
    โ€ข Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[โœ“] Android Studio (version 4.0)
    โ€ข Android Studio at /Applications/Android Studio.app/Contents
    โ€ข Flutter plugin version 48.1.2
    โ€ข Dart plugin version 193.7547
    โ€ข Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)

[โœ“] VS Code (version 1.48.2)
    โ€ข VS Code at /Applications/Visual Studio Code.app/Contents
    โ€ข Flutter extension version 3.14.0

[โœ“] Connected device (4 available)
    โ€ข iPhone 11 (mobile) โ€ข EDB19945-0B9F-4A6E-BAA1-425E68E3B6D6 โ€ข ios
      โ€ข com.apple.CoreSimulator.SimRuntime.iOS-13-7 (simulator)
    โ€ข macOS (desktop)    โ€ข macos                                โ€ข darwin-x64
      โ€ข Mac OS X 10.15.6 19G2021
    โ€ข Web Server (web)   โ€ข web-server                           โ€ข web-javascript
      โ€ข Flutter Tools
    โ€ข Chrome (web)       โ€ข chrome                               โ€ข web-javascript
      โ€ข Google Chrome 85.0.4183.83

โ€ข No issues found!

@oakstair I had a few 3rd party packages in my project. Thank you for making the miniproject!

@oakstair I had a few 3rd party packages in my project. Thank you for making the miniproject!

No problem! When I studied (long time age) I didn't even have any time for studying :-)

@oakstair I had a few 3rd party packages in my project. Thank you for making the miniproject!

No problem! When I studied (long time age) I didn't even have any time for studying :-)

That's understandable! App development is a hobby of mine, so I enjoy every moment of it, especially with Flutter :)

So... Has anyone managed to run unit tests with FlutterFire ever? I'm getting the same issue and I don't even know if it is meant to work during unit testing or if I should give up the idea of testing...

I am also facing this issue. Everything runs fine except in testing.

So I faced a similar issue in testing. I was attempting to create Document References during testing and calling await Firebase.initializeApp(); inside my setUpAll()

In my case I solved this by pulling the following file into my test directory which hooks into mock call handlers to be used throughout the test: https://github.com/FirebaseExtended/flutterfire/blob/master/packages/cloud_firestore/cloud_firestore/test/mock.dart

For reference to contents of file:

// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:firebase_core_platform_interface/firebase_core_platform_interface.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

typedef Callback(MethodCall call);

setupCloudFirestoreMocks([Callback customHandlers]) {
  TestWidgetsFlutterBinding.ensureInitialized();

  MethodChannelFirebase.channel.setMockMethodCallHandler((call) async {
    if (call.method == 'Firebase#initializeCore') {
      return [
        {
          'name': defaultFirebaseAppName,
          'options': {
            'apiKey': '123',
            'appId': '123',
            'messagingSenderId': '123',
            'projectId': '123',
          },
          'pluginConstants': {},
        }
      ];
    }

    if (call.method == 'Firebase#initializeApp') {
      return {
        'name': call.arguments['appName'],
        'options': call.arguments['options'],
        'pluginConstants': {},
      };
    }

    if (customHandlers != null) {
      customHandlers(call);
    }

    return null;
  });
}

Then in each test I would do the following:

void main() {
  setupCloudFirestoreMocks();

  setUpAll(() async {
    TestWidgetsFlutterBinding.ensureInitialized();

    await Firebase.initializeApp();
  });

// ALL MY TESTS WOULD BE HERE

}

I think the flutter test is running on neither iOS nor Android, so what if we're talking about running it in a macos environment?

https://github.com/FirebaseExtended/flutterfire/blob/master/packages/cloud_firestore/cloud_firestore/pubspec.yaml#L16

flutter testใฎใจใใฏiOSใงใ‚‚Androidใงใ‚‚ใชใ„็’ฐๅขƒใงๅ‹•ใ„ใฆใ„ใ‚‹ใจๆ€ใ„ใพใ™ใ€‚macosใฎ็’ฐๅขƒใงๅ‹•ใ‹ใ™ใจใ„ใ†ใ“ใจใซใ—ใŸใ‚‰ใฉใ†ใงใ—ใ‚‡ใ†ใ‹๏ผŸ

So I faced a similar issue in testing. I was attempting to create Document References during testing and calling await Firebase.initializeApp(); inside my setUpAll()

In my case I solved this by pulling the following file into my test directory which hooks into mock call handlers to be used throughout the test: https://github.com/FirebaseExtended/flutterfire/blob/master/packages/cloud_firestore/cloud_firestore/test/mock.dart

For reference to contents of file:

// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:firebase_core_platform_interface/firebase_core_platform_interface.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

typedef Callback(MethodCall call);

setupCloudFirestoreMocks([Callback customHandlers]) {
  TestWidgetsFlutterBinding.ensureInitialized();

  MethodChannelFirebase.channel.setMockMethodCallHandler((call) async {
    if (call.method == 'Firebase#initializeCore') {
      return [
        {
          'name': defaultFirebaseAppName,
          'options': {
            'apiKey': '123',
            'appId': '123',
            'messagingSenderId': '123',
            'projectId': '123',
          },
          'pluginConstants': {},
        }
      ];
    }

    if (call.method == 'Firebase#initializeApp') {
      return {
        'name': call.arguments['appName'],
        'options': call.arguments['options'],
        'pluginConstants': {},
      };
    }

    if (customHandlers != null) {
      customHandlers(call);
    }

    return null;
  });
}

Then in each test I would do the following:

void main() {
  setupCloudFirestoreMocks();

  setUpAll(() async {
    TestWidgetsFlutterBinding.ensureInitialized();

    await Firebase.initializeApp();
  });

// ALL MY TESTS WOULD BE HERE

}

thank you for sharing, this fixed the error for me

Was this page helpful?
0 / 5 - 0 ratings