如何使用.find方法以角度获取数组的覆盖率。我在下面的代码段遇到了麻烦。
sample.component.ts
public permissions = [{id: 0, value: 'fruit' }, {id: 1, value: 'vegetable'}];
this.filteredList = this.permissions.slice();
public isFiltered(permission) {
return this.filteredList.find(item => item.id === permission.id);
}
sample.spec.ts
it('should call myMethod ', () => {
expect(component.isFiltered(1)).toEqual(true);
});
我遇到以下错误,TypeError: Cannot read property 'find' of undefined
任何帮助将不胜感激
这样写
it('should call myMethod ', () => {
public permissions = [{id: 0, value: 'fruit' },{id: 1, value: 'vegetable'} ];
component.filteredList = this.permissions.slice();
expect(component.isFiltered(1)).toEqual(true);
});
您需要给component.filteredList
一些值