модульное тестирование: сохранение токена устройства в базе данных - PullRequest
0 голосов
/ 14 февраля 2020

Я пытаюсь протестировать этот метод, который сохраняет токен устройства в 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>

Что вызывает эту ошибку и как я могу решить это?

...