【问题标题】:How can I access the Firebase Flutter plugin from a test?如何从测试中访问 Firebase Flutter 插件?
【发布时间】:2019-04-21 05:42:27
【问题描述】:

我想在 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

我该如何解决这个问题?

【问题讨论】:

  • 你好。你有没有找到解决这个问题的方法?我还想用真正的 firebase 实例进行测试

标签: firebase testing plugins firebase-authentication flutter


【解决方案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的最后一段

【讨论】:

  • 感谢 Gunter,但正如我所说,我想测试一个真正的 Firebase(不是模拟)。如果我使用 flutter run 而不是 flutter test 可能会有效?
  • 然后你必须在模拟器上运行测试。是的,flutter run 会起作用。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2020-06-26
    • 1970-01-01
    • 2020-08-09
    • 2020-09-02
    • 1970-01-01
    • 2020-06-05
    • 2019-07-26
    • 1970-01-01
    • 2021-04-29
    相关资源
    最近更新 更多