如何通过测试访问Firebase Flutter插件?

时间:2018-11-18 20:00:30

标签: firebase testing plugins firebase-authentication flutter

我想在Flutter测试期间运行真正的Firebase(而不是模拟游戏)。我正在尝试使用FirebaseOptions对Firebase进行身份验证:

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');
    });
  });
}

但是,出现以下错误:

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

我该如何解决?

2 个答案:

答案 0 :(得分:1)

插件仅在移动设备(或仿真器)上运行。
为了使使用插件的测试代码成为可能,您可以注册自己的处理程序,以响应方法通道请求,就像插件的本机端一样

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');
});

https://medium.com/flutter-io/flutter-platform-channels-ce7f540a104e的最后一部分开始

答案 1 :(得分:0)

这将检查当前的Firebase用户是否为null。我还是在写测试。如果可能,将更新signInAnonymous的测试。

test('API current Firebase user', () async {
      MethodChannel channel = MethodChannel(
        'plugins.flutter.io/firebase_auth',
      );
      channel.setMockMethodCallHandler((MethodCall call) async {
        if (call.method == 'currentUser') {
          return ;
        }
        ;
        throw MissingPluginException();
      });

      FirebaseUser user = await FirebaseAuth.instance.currentUser();
      expect(user, null);
    });