I have a simple function that does this
ngOnInit() {
if (this.session.getToken()) {
this.isUserLogged = true;
}
this.loadingObserver = this.session.loadingObservable.subscribe(loading => this.isLoading = loading);
}
and my test is as follows
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();
}));
But in the code coverage of my application, the subscribe isn't covered. In fact, expecting false also works.
Do you have any idea on how to test a subscription ?