I have implemented a straightforward function that accomplishes this task.
ngOnInit() {
if (this.session.getToken()) {
this.isUserLogged = true;
}
this.loadingObserver = this.session.loadingObservable.subscribe(loading => this.isLoading = loading);
}
Here is the test scenario I have devised:
it('Testing ngOnInit() ...', async(() => {
let spy = spyOn(services.session, 'getToken').and.returnValue('token');
services.session.loadingObservable.subscribe(any => expect(component.isLoading).toEqual(false));
component.ngOnInit();
expect(component.isUserLogged).toEqual(true);
expect(spy).toHaveBeenCalled();
}));
However, during code coverage analysis of my application, the subscribe portion remains uncovered. Interestingly, expecting false also seems to yield the desired result.
Does anyone have suggestions on how to effectively test a subscription in this context?