Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot read property 'pipe' of undefined - Angular 6

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 ?

like image 823
su1212 Avatar asked Oct 14 '25 04:10

su1212


1 Answers

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
like image 176
Abel Avatar answered Oct 17 '25 22:10

Abel