FunctionToTest() {
this.someService.method1().subscribe((response) => {
if (response.Success) {
this.someService.method2().subscribe((res) => {
this.anotherService.method3();
})
}
});
}
Consider the following scenario.
Key points to test:
someService.method1()
should be called.- If response.Success is true, then verify if
someService.method2()
was invoked. - Ensure that
anotherService.method3()
was triggered whensomeService.method2()
returned a value.
Testing the first point is straightforward.
let spy = spyOn(someService, 'method1');
expect(spy).toHaveBeenCalled();
But how about testing the second and third points?
let spy= spyOn(someService, 'method1').and.returnValue({subscribe: () => {success:true}});
subject.FunctionToTest();
Is this approach correct?