Component.ts
// Component containing function
Public myData(){
this.myService.getData()
.pipe(
take(1),
catchError((err: any) => {
this.myRows = [];
return throwError(err);
}),
finalize(() => {
console.log('In Finally');
})
)
.subscribe((rows) => {
this.myRows = rows;
// Do something
});
}
myService.ts
// Service Call
public getData(): Observable < customer[] > {
const url = 'some url';
return this.http.get(url).pipe(
map(this.checkForError),
catchError((err: HttpErrorResponse) => {
return throwError(err);
}),
map(this.myJson),
share()
);
}
spec.ts
// Test Case
it('should test', () => {
let test = spyOn(myService, 'getData');
component.myData();
expect(test).toHaveBeenCalled();
});
Couldn't resolve this error. Not able to find an easy way of writing test case for service call. How can we resolve the pipe error ?
The error seems to be on this.myService.getData().pipe(...
Because in your test, you spy on "getData" but you are not returning any value and particularly not an Observable from the spy.
What you may want to do in your test :
const customers = []; // define here your testing array
spyOn(myService, 'getData').and.returnValue(of(customers)); // "of" is a Rxjs function to create an Observable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With