Angular 4 管道单元测试不适用于基本管道
Angular 4 pipe unit test not working for basic pipe
Angular 4 管道单元测试不适用于基本管道
Karma error I get: TypeError: Cannot read property '0' of undefined
这是我的烟斗(非常基础):
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);
});
});
首先更新您的转换函数,测试将使用此代码
export class MyFilter implements PipeTransform {
transform( items: Array<any> ) {
return listOfItems;
}
}
Angular 4 管道单元测试不适用于基本管道
Karma error I get: TypeError: Cannot read property '0' of undefined
这是我的烟斗(非常基础):
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);
});
});
首先更新您的转换函数,测试将使用此代码
export class MyFilter implements PipeTransform {
transform( items: Array<any> ) {
return listOfItems;
}
}