单元测试:将设备令牌保存到Firebase

时间:2020-02-13 21:04:49

标签: flutter dart mockito flutter-test

我正在尝试测试将该设备令牌保存到Firestore的方法。

save_token.dart

class Notification {
  void saveDeviceToken() async {
    FirebaseUser user = await firebaseAuth.currentUser();
    await fcm.getToken().then((token) {
      db.collection('users').document(user.email).setData({
        'device_token': token,
        'createdAt': FieldValue.serverTimestamp(),
      });
    });
  }
}

这是我到目前为止尝试过的: save_token_test.dart

test('should save device token', () {
final Notification notification = Notification();
final firebaseAuth = FirebaseAuth();

      when(firebaseAuth.currentUser())
          .thenAnswer((_) => Future<FirebaseUser>.value(firebaseUser));
      when(firebaseMessaging.getToken())
          .thenAnswer((_) => Future<dynamic>.value('SOMETOKEN'));
      when(firestore.collection('users').document(any).setData(any))
          .thenAnswer((realInvocation) => null);
      notification.saveDeviceToken();
    });

但是我遇到了这个错误

00:02 +1 -1: Notification Setup should save device token [E]                                            
  NoSuchMethodError: The method 'document' was called on null.
  Receiver: null
  Tried calling: document(null)
  dart:core                                                     Object.noSuchMethod
  tests/unit_tests/shared/widgets/notification_test.dart 64:42  main.<fn>.<fn>

什么原因导致此错误,我该如何解决?

0 个答案:

没有答案