Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access the Firebase Flutter plugin from a test?

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?

like image 391
Eduardo Yamauchi Avatar asked Dec 18 '25 17:12

Eduardo Yamauchi


1 Answers

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

like image 93
Günter Zöchbauer Avatar answered Dec 20 '25 10:12

Günter Zöchbauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!