I’m trying to write a negative test for my AuthService that a property in the constructor is set to null if nothing is returned from angularFireAuth. The positive side of this test case is working, and if I modify my mock file directly I can see the outcomes I want. Even with the spyOnProperty mock I have below, when I subscribe to it I can see in console logs that it’s returning null.
The problem is that I can’t get the actual service constructor to pick up the spyOnProperty returnValue. For a component I would do a fixture.detectChanges but that doesn’t work for a service. Is there some way I have to rebuild the service before the constructor picks up the mocked null value?
AuthService Constructor
{
(this.partyCollectionPath);
this.afAuth.authState.subscribe((user: User | null) => {
if (user) {
this.userData = {
uid: user.uid,
email: user.email,
emailVerified: user.emailVerified
};
this.liveUid = user.uid
} else {
this.userData = null
}
});
}
AngularFireAuthMock
import { User } from '../shared/interface/user';
import { of } from 'rxjs';
const authState: User = {
uid: 'fakeuser',
email: '[email protected]',
emailVerified: true
};
export const AngularFireAuthMock: any = {
AngularFireAuth: jasmine.createSpyObj('AngularFireAuth', {
//other functions
}),
get authState() {
return of(authState);
},
Test Assertion
it('userData set to null', () => {
const spy = spyOnProperty(AngularFireAuthMock, 'authState', 'get');
spy.and.returnValue(of(null))
AngularFireAuthMock.authState.subscribe((data: any) => {
console.log('subscribe: ', data)
})
console.log('userData: ', service.userData)
// expect(service.userData).toBeNull()
});
Console Logs
LOG: ‘subscribe: ‘, null
LOG: ‘userData: ‘, Object{uid: ‘fakeuser’, email: ‘[email protected]’,
emailVerified: true}