首页 > 解决方案 > 无法对 cloud_firestore 进行实时单元测试

问题描述

我目前正在尝试编写一个单元测试来验证是否存在一定数量的文档。

这是我到目前为止所拥有的

  test('Login with no account', () async {
    Firestore _firestore = Firestore.instance;
    final QuerySnapshot result = await _firestore
        .collection(UserFirestoreField.Collection)
        .where(UserFirestoreField.EmailAddress, isEqualTo: 'email@example.com')
        .where(UserFirestoreField.Password, isEqualTo: 'wrongpassword')
        .getDocuments();
    final List<DocumentSnapshot> docs = result.documents;
    print(docs);
  });

我得到的错误是

包:flutter/src/services/platform_channel.dart 314:7 MethodChannel.invokeMethod

MissingPluginException(在通道 plugins.flutter.io/cloud_firestore 上找不到方法 Query#getDocuments 的实现)

我的 android 模拟器在我的应用程序启动时运行。

我见过的每个指南都在谈论模拟数据库,我想实际检查真实的数据库。

有什么方法可以在飞镖/颤振中做到这一点?

谢谢!

标签: unit-testingdartgoogle-cloud-platformgoogle-cloud-firestore

解决方案


在 Flutter 中,单元和小部件测试在您的主机上运行,​​该主机没有您的 firebase 插件的本机部分。这就是您收到此错误的原因。

您确实应该在测试中模拟数据库,但如果您真的想测试您的应用程序,使其接近用户运行的方式,您将在模拟器上运行集成测试。

您还可以使用基于 dart 的 Firebase 插件或使用 Firebase REST API。

您可以在此处找到更多相关信息:https ://flutter.dev/docs/testing


推荐阅读