如何从测试中访问Firebase颤振插件?

如何从测试中访问Firebase颤振插件?,firebase,testing,plugins,firebase-authentication,flutter,Firebase,Testing,Plugins,Firebase Authentication,Flutter,我想在颤振测试期间运行真正的Firebase(而不是模拟)。我正在尝试使用Firebase选项对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.dar

我想在颤振测试期间运行真正的Firebase(而不是模拟)。我正在尝试使用
Firebase选项对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');
    });
  });
}

如何解决此问题?

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


的最后一部分,这将检查当前firebase用户是否为空。我仍然在写测试。如果可能,将以匿名方式更新测试

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

谢谢Gunter,但正如我所说,我想测试一个真正的Firebase(而不是模拟)。如果我使用
flatterrun
而不是
flattertest
,它可能会起作用,那么您必须在模拟器上运行测试。是的,使用
flatter run
它可以工作。您好。你有没有找到解决办法?我还想用一个真正的firebase实例进行测试
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');
});
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);
    });