【问题标题】:How to write the test for a Model that uses firestore documentsnapshot如何为使用 Firestore 文档快照的模型编写测试
【发布时间】:2021-08-17 04:43:33
【问题描述】:

目前我正在关注 TDD,但现在我想测试一个基于文档快照发出 UserEntity 的 UserModel 类。这是我要测试的代码。

  factory UserModel.fromSnapsot(DocumentSnapshot snapshot) {
    return UserModel(
      email: snapshot.get('email').data(),
      username: snapshot.get('username').data(),
      uid: snapshot.get('uid').data(),
    );
  }

我写了一个测试,但它说失败了

在 null 上调用了“get”方法。接收方:空。尝试调用:get("email")

我写的代码是


  group('fromSnapshot', () {
    when(UserModel.fromSnapsot(mockDocumentSnapshot))
        .thenAnswer((_) => tUserModel);

    final result = UserModel.fromSnapsot(mockDocumentSnapshot);

    expect(result, tUserModel);
  });

知道如何测试上述代码吗?

【问题讨论】:

  • mockDocumentSnapshot 为空,它必须是有效的快照,而不是空
  • 有没有办法用数据制作自定义快照
  • 我不知道,查看DocumentSnapshot官方文档

标签: flutter unit-testing dart google-cloud-firestore flutter-test


【解决方案1】:

我找到了一个名为 fake_cloud_firestore 的插件。这样我就可以用假数据通过测试了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-16
    • 2020-09-26
    • 2021-09-27
    • 2019-08-02
    • 2022-01-25
    • 2018-08-22
    • 2021-07-13
    • 1970-01-01
    相关资源
    最近更新 更多