firebase - 如何从测试中访问 Firebase Flutter 插件?

标签 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

我该如何解决这个问题?

最佳答案

插件只能在移动设备(或模拟器)上运行。
为了使使用插件的测试代码成为可能,您可以注册自己的处理程序来响应方法 channel 请求,就像插件的 native 端一样

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 的最后一节

关于firebase - 如何从测试中访问 Firebase Flutter 插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53364890/

相关文章:

debugging - 除了使用浏览器之外,是否有更好的方法来测试或调试 Mod 重写规则?

jquery - 什么是 "must have"jQuery 插件?

java - 我如何访问本地文件系统的资源并从浏览器操作它们

node.js - 如何观察firebase实时数据库的变化

javascript - 删除与某个子值关联的整个记录

maven - 与 surefire 和 testng 并行运行测试

javascript - 同时测试两个不同的 npm 包版本

android - Firebase Android : How to get list inside specific child?

firebase - 如何使用 CLI 在 Firebase 项目中创建/初始化默认实时数据库实例?

grails - GR8 CRM 核心插件错误无法编译