无法覆盖以下代码行?
this.dataSource.sortingDataAccessor = (item, property) => {
switch (property) {
case 'status': return item.status;
case 'description': return item.description;
default: return item[property];
}
};
不确定您是否找到了解决方案,但我也需要与您类似的测试覆盖率。我希望这对您或其他也在为此苦苦挣扎的人有所帮助。
示例1(component.ts)
this.dataSource.sortingDataAccessor = (item: any, property: any) => {
switch (property) {
case 'card.age': return item.value.card.age;
default: return item.value[property];
}
};
示例 1(组件.spec.ts)
it('should sort by "card.age" when property is "card.age"', () => {
const mockData = { value: { card: { name: 'card 1', age: '2' } } };
const result = component.dataSource.sortingDataAccessor(mockData, 'card.age');
expect(result).toBe('2');
});
示例2(component.ts)
this.dataSource.sortingDataAccessor =
(data: AbstractControl, sortHeaderId: string) => {
const value: any = data.value[sortHeaderId];
return typeof value === "string" ? value.toLowerCase() : value;
};
示例 2(组件.spec.ts)
it('should use sorting data accessor', () => {
const mockData = { value: { name: 'example name' } };
const sortingDataAccessor = component.dataSource.sortingDataAccessor;
const result = sortingDataAccessor(mockData, 'name');
expect(result).toBe('example name');
});
在示例 1 中,在我的例子中,我在 FormArray 中有一堆 FormGroup,因此我必须首先获取该项目的“值”才能访问实际项目及其属性。