Angular 无法读取未定义的-6的属性“pipe”
组件技术Angular 无法读取未定义的-6的属性“pipe”,angular,unit-testing,jasmine,angular6,karma-jasmine,Angular,Unit Testing,Jasmine,Angular6,Karma Jasmine,组件技术 // Component containing function Public myData(){ this.myService.getData() .pipe( take(1), catchError((err: any) => { this.myRows = []; return throwError(err); })
// 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()
);
}
无法解决此错误。无法找到为服务调用编写测试用例的简单方法。如何解决管道错误?您是否尝试过以下异步函数: 它“应该测试”,fakeAsync=>{//Add fakeAsync 让test=spyOnmyService,‘getData’; component.myData; 勾选;//添加这个 expecttest.to已装入; } 错误似乎在这个.myService.getData.pipe上。。。 因为在您的测试中,您监视getData,但不返回任何值,特别是不从spy返回可观察值 您可能希望在测试中执行的操作:
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"
})
尝试了上面提供的解决方案,但仍然得到相同的错误。使用这种方法,测试用例将通过,但代码覆盖率将不会发生。还有其他方法吗?如果您正在编写单元测试,那么不应该在组件测试中真正调用您的服务。相反,您应该为组件编写单元测试,为服务编写单元测试。这种方法使测试更易于编写、阅读,从而更易于维护。另一方面,如果您真的想调用服务方法,则必须正确地模拟This.http.geturl,因为您不想进行真正的http调用。例如,请参阅本文模拟http调用,这与问题无关。rxjs管道和角管道完全不同
import { Component, OnInit, Input, Pipe } from '@angular/core';
@Pipe({ //use for filter pipe
name: "dataFilter"
})