【问题标题】:Unable to unit test cloud_firestore live无法对 cloud_firestore 进行实时单元测试
【发布时间】:2020-01-15 04:05:56
【问题描述】:

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

这是我目前所拥有的

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

我得到的错误是

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

MissingPluginException(没有找到方法的实现 Query#getDocuments on channel plugins.flutter.io/cloud_firestore)

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

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

在 dart/flutter 中有什么方法可以做到这一点?

谢谢!

【问题讨论】:

    标签: unit-testing dart google-cloud-platform google-cloud-firestore


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 我为什么不应该使用实时数据库?
    • @LongTSQLFiles:大多数开发人员认为“单元测试”应该在本地运行而不依赖于基础设施(该单元完全可以与例如模拟或测试替身或你有什么一起工作)。一些开发人员使用“集成测试”(尽管有其优点)来测试基础设施。集成测试 Firebase/Flutter 平台的问题是身份验证在测试模式下不起作用。完全没有。 known issue 希望很快得到解决。
    【解决方案2】:

    您可以 JsonDecode 到本地地图并测试地图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-13
      • 1970-01-01
      • 1970-01-01
      • 2018-10-17
      • 2014-06-03
      • 1970-01-01
      • 2012-01-08
      • 1970-01-01
      相关资源
      最近更新 更多