Angular 4管道单元测试不适用于基本管道
我得到的Karma错误:TypeError:无法读取未定义的属性“0”
这是我的管道(非常基础):
import { Pipe, PipeTransform } from "@angular/core";
@Pipe({
name: "userPipe",
pure: true
})
export class MyFilter implements PipeTransform {
transform(items: any, [listOfItems]: any) {
// Example of a pipe: listOfItems = listOfItems.filter(item => item.type === "test");
return listOfItems;
}
}
这是我的管道单元测试:
import { MyFilter } from "./user.pipe";
describe("userPipe", () => {
let pipe: MyFilter;
let fakeResponse = ["abc"];
beforeEach(() => {
pipe = new MyFilter();
});
it("transforms abc to abc", () => {
expect(pipe.transform(fakeResponse)).toEqual(fakeResponse);
});
});
答案 0 :(得分:1)
首先更新您的转换函数,test将使用此代码
export class MyFilter implements PipeTransform {
transform( items: Array<any> ) {
return listOfItems;
}
}