Eu estou tentando testar este método que salva um dispositivo de token para 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(),
});
});
}
}
Aqui está o que eu tentei até agora: 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();
});
Mas eu estou recebendo este erro
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>
O que causa esse erro e como posso resolver isso?