I’m trying to mock/test SendVerificationMail()
from my AuthService, which starts by calling the AngularFireAuth method currentUser
which returns an angularFire User object, which you can then use to call sendEmailVerification(). I’ve tried a few different mocking methods, but I just end up with the errors below because it can’t find user.sendEmailVerification
Error
Error: Uncaught (in promise): TypeError: user.sendEmailVerification is not a function TypeError: user.sendEmailVerification is not a function
AuthService
SendVerificationMail() {
return this.afAuth.currentUser
.then((user: any) => {user.sendEmailVerification(),})
.then(() => {
this.router.navigate(['verify-email-address']);
});
}
AngularFireAuth Mock File
import { User } from '../shared/interface/user';
import { of } from 'rxjs';
import { userCredential } from './mock_userCredential';
const authState: User = {
uid: 'fakeuser',
email: '[email protected]',
emailVerified: true
};
export const AngularFireAuthMock: any = {
createUserWithEmailAndPassword: () => {
return Promise.resolve(userCredential)
},
sendEmailVerification: () => {
return Promise.resolve()
},
AngularFireAuth: jasmine.createSpyObj('AngularFireAuthMock', {
'sendEmailVerification': {
function() {
return Promise.resolve({
code: 'auth/user-not-found'
})
}
}
}
),
get authState() {
return of(authState);
},
get user() {
return this.authState;
},
get currentUser() {
return Promise.resolve(userCredential)
}
}
auth.service.spec.ts
it('SendVerificationMail() - Functions Called', fakeAsync(() => {
const spySendEmailVerification = spyOn(AngularFireAuthMock, 'sendEmailVerification')
service.SendVerificationMail()
tick();
expect(spySendEmailVerification).toHaveBeenCalled()
}));