简体   繁体   English

Flutter:通过存储库对 Firebase Firestore 进行单元测试

[英]Flutter: Unit testing Firebase Firestore through Repository

I have a purpose to test my Firestore repository methods.我的目的是测试我的 Firestore 存储库方法。 I found out a useful fake_cloud_firestore package, which helps to mock firestore.我发现了一个有用的fake_cloud_firestore包,它有助于模拟 firestore。

I have a repository method that I want to test:我有一个要测试的存储库方法:

@override
  Future<Either<UserFailure, UserDTO>> updateUser(FUser user) async {
    try {
      final UserDTO userDTO = UserDTO.fromDomainForUpdatingProfile(user);

      final Map<String, dynamic> userJson = userDTO.toJson();

      await _firestore
          .collection(FirestoreCollections.COLLECTION_USERS)
          .doc(user.id)
          .set(userJson, SetOptions(merge: true));

      return right(userDTO);
    } on FirebaseException catch (e) {
      if (e.message!.contains('PERMISSION_DENIED')) {
        return left(const UserFailure.insufficientPermission());
      } else {
        return left(const UserFailure.unexpected());
      }
    } on Exception {
      return left(const UserFailure.unexpected());
    }
  }

And a test:还有一个测试:

test('exception test', () async {
        final Map<String, dynamic> jsonMap = json.decode(
          fixture("user/full_domain_fuser.json"),
        );

        final FUser fuser = FUser.fromJson(jsonMap);
        final userJson = UserDTO.fromDomainForUpdatingProfile(fuser).toJson();

        when(fakeFirebaseFirestore
                .collection(FirestoreCollections.COLLECTION_USERS)
                .doc(fuser.id)
                .set(userJson, SetOptions(merge: true)))
            .thenThrow(Exception());

        final result = await userRepository.updateUser(fuser);

        expect(result, const Left(UserFailure));
      });
    });

What I want to do is throw an Exception when calling Firestore and updating the document, but when() part is not being triggered.我想要做的是在调用 Firestore 和更新文档时抛出异常,但是 when() 部分没有被触发。 When running a test I get:运行测试时,我得到:

Bad state: No method stub was called from within `when()`. Was a real method called, or perhaps an extension method?

FakeFirebaseFirestore and UserRepository construction: FakeFirebaseFirestore 和 UserRepository 构建:

FakeFirebaseFirestore fakeFirebaseFirestore = FakeFirebaseFirestore();
UserRepository userRepository = UserRepository(fakeFirebaseFirestore);

It looks like that my firestore call looks the same in a test and also in repository method.看起来我的 firestore 调用在测试和存储库方法中看起来都一样。 What am I missing in this when() part?我在这个 when() 部分缺少什么?

The error is coming up because you're using an object that does not extend the Mock class from the mockito package where the when function comes from.出现错误是因为您使用的对象没有从when函数来自的mockito包中扩展Mock类。

You can write the Firestore logic in a separate util class and mock that class and test against that.您可以在单独的 util 类中编写Firestore逻辑并模拟该类并对其进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM