I have a function to be unit tested that I'm not sure how to approach. Simplified:
someFunction(): boolean {
this.service.login().subscribe(response => {
if (response) {
return someOtherFunction();
}
});
}
someOtherFunction(): boolean {
this.service.otherTask().subscribe(response => {
if (response) {
return true;
}
});
}
I want to test the result of someFunction
in this case. However, this does not work:
describe('someFunction', () => {
it('returns true', () => {
serviceSpy.login.and.returnValue(of({response: response}));
serviceSpy.otherTask.and.returnValue(of({response: otherResponse}));
result = component.someFunction();
expect(result).toEqual(true);
});
});
ServiceSpy has been configured before this block.
I can see that the functions are executed and that true is returned. However, at the moment I am asking for result
, it is still undefined. The test framework does not wait for everything to be completed. I have tried to use async, fakeAsync, done(), but these do not do the trick.
Is there a way to test the return value of someFunction
?