How can I access the Firebase Flutter plugin from a test?
1,698
Plugins run only on mobile devices (or emulators).
To make testing code that uses plugins possible, you can register your own handlers that respond to method channel requests like the native side of plugins would
test('gets greeting from platform', () async { const channel = MethodChannel('foo'); channel.setMockMethodCallHandler((MethodCall call) async { if (call.method == 'bar') return 'Hello, ${call.arguments}'; throw MissingPluginException(); }); expect(await hello('world'), 'Platform says: Hello, world'); });
From the last section of https://medium.com/flutter-io/flutter-platform-channels-ce7f540a104e
Author by
Eduardo Yamauchi
Updated on December 07, 2022Comments
-
Eduardo Yamauchi over 1 year
I want to run the real Firebase (not a mock) during a Flutter test. I'm trying to authenticate Firebase with
FirebaseOptions
:import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:firebase_core/firebase_core.dart'; import "package:test/test.dart"; Future<void> main() async { final FirebaseApp app = await FirebaseApp.configure( name: 'test', options: const FirebaseOptions( googleAppID: 'xxxxxxx', projectID: 'yyyyyy', ), ); final Firestore firestore = Firestore(app: app); await firestore.settings(timestampsInSnapshotsEnabled: true); test('Testing access.', () async { final FirebaseAuth _auth = FirebaseAuth.instance; FirebaseUser user = await _auth.signInAnonymously(); firestore.collection('aaaaa').document('bbbbb').get().then((documentSnaphot) { expect(documentSnaphot['xxxx'], 'ccccc'); }); }); }
However, I'm getting the following error:
Failed to load "C:\Users\Ed\testing\app\test\user_test.dart": MissingPluginException(No implementation found for method FirebaseApp#appNamed on channel plugins.flutter.io/firebase_core) package:flutter/src/services/platform_channel.dart 278:7 MethodChannel.invokeMethod ===== asynchronous gap =========================== c: 38:53 FirebaseApp.appNamed ===== asynchronous gap =========================== c: 64:55 FirebaseApp.configure ===== asynchronous gap =========================== test\usuario_test.dart 7:45 main ===== asynchronous gap =========================== package:test serializeSuite ..\..\..\AppData\Local\Temp\flutter_test_listener.64a30b51-eb69-11e8- a427-1831bf4c06e8\listener.dart 19:27 main
How can I solve this?
-
Eduardo Yamauchi over 5 yearsThanks Gunter, but as I said, I want to test a real Firebase (not a mock). Maybe it works if I use
flutter run
instead offlutter test
? -
Günter Zöchbauer over 5 yearsThen you have to run the test on an emulator. Yes with
flutter run
it will work.