I am currently working on creating a negative test scenario for my AuthService. The specific test case involves verifying that a property in the constructor is set to null if the angularFireAuth does not return any data. While the positive test case is functioning as expected, I am encountering some challenges with setting up the negative test scenario. Even with the spyOnProperty mock implementation, I am observing that the property is returning null when I subscribe to it.
The issue seems to be related to getting the service constructor to recognize the mocked null value from the spyOnProperty. In the case of a component, I would typically use fixture.detectChanges, but this approach does not work for a service. Is there a specific method or technique that I should be using to properly rebuild the service so that the constructor can pick 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: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2a5e4f595e6a5e4f595e04494547">[email protected]</a>',
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}