无法读取未定义的 属性 'pipe' - Angular 6

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();
    });

无法解决此错误。无法找到编写服务调用测试用例的简单方法。我们如何解决管道错误?

你试过像这样的异步函数吗:

it('should test', fakeAsync(() => { // Add FakeAsync
    let test =  spyOn(myService, 'getData');
    component.myData();
    tick(); // Add this
    expect(test).toHaveBeenCalled();
}))

错误似乎在 this.myService.getData().pipe(...
因为在您的测试中,您监视 "getData" 但您没有返回任何值,尤其是没有返回来自间谍的 Observable。

你可能想在测试中做什么:

const customers = []; // define here your testing array
spyOn(myService, 'getData').and.returnValue(of(customers)); // "of" is a Rxjs function to create an Observable

试试这个:

声明管道在 ts 文件中

import { Component, OnInit, Input, Pipe } from '@angular/core';

@Pipe({                   //use for filter pipe
  name: "dataFilter"
})