[英]Unit testing component throwing transform is not a function for a pipe
in my component class i have following get method:在我的组件类中,我有以下 get 方法:
get getCSVForDownload() {
return defer(() => {
this.downloadingData = true;
return this.csvRepoService.getCSV(this.csvId).pipe(
map((items: ICSV[]) => {
let filteredItems = items;
filteredItems = this.filterDateRangePipe.transform(filteredItems, this.timeFilterRange, 'createdDate');
return filteredItems;
}),
);
});
in the spec file of that component first I have created a mock pipe like this:在该组件的规范文件中,我首先创建了一个像这样的模拟管道:
@Pipe({ name: 'filter' })
class MockPipe implements PipeTransform {
public transform(items: any[], timeFilterRange: TimeFilterRange, fieldName: string): any {
return items;
}
}
And in the beforeEach i have provided the pipe in following way:在 beforeEach 中,我以以下方式提供了管道:
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ABCComponent],
imports: [
HttpClientTestingModule,
],
providers: [
{ provide: FilterDateRangePipe, useValue: MockPipe },
{ provide: SecurityService, useValue: mockSecurityService }
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
mockCsvRepoService = TestBed.inject(CsvRepoService );
spyOn(mockCsvRepoService, 'getCSV').and.returnValue(of(testCSVs));
});
And the test I have written is following:我写的测试如下:
it('Should call getCSV when downloadCSV triggered', fakeAsync(() => {
component.getCSVForDownload.subscribe()
expect(mockCsvRepoService.getCSV).toHaveBeenCalled()
flush();
}));
Though I have provided mock object for FilterDateRangePipe which has transform method in it, I am still getting error like following虽然我已经为 FilterDateRangePipe 提供了具有转换方法的模拟对象,但我仍然收到如下错误
TypeError: this.filterDateRangePipe.transform is not a function
What I am missing?我错过了什么?
要模拟管道,您应该通过useClass
而不是 useValue 提供useValue
。
{ provide: FilterDateRangePipe, useClass: MockPipe },
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.