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