如何编写 angular 测试用例来覆盖 array.find()
How to write angular test case to cover array.find()
我正在尝试为以下方法编写测试用例。我写了类似下面的内容,但它并没有提高/更改代码覆盖率。有人可以帮助我做错什么吗?
组件
remove(item: string, element:any){
const found = this.list1.find(el => el.team == element.tirname);
if(found){
var index = this.list1.indexOf(found);
if (index >= 0) {
this.list1[index].removeFlag = true;
const index1= this.list1[index].orgsUpdated.indexOf(item);
if (index1 >= 0) {
this.list1[index]['orgsUpdated'].splice(index1, 1);
this.list1[index]['removeFlag'] = false;
}
}
}
const found1 = this.list2.find(el => el.name == element.name);
if(found1){
var index2 = this.list2.indexOf(found1);
if (index2 >= 0) {
const index1= this.list2[index2].gOrgNames.indexOf(item);
if (index1 >= 0) {
this.list2[index]['gitOrganizationNames'].splice(index1, 1);
}
}
}
}
规格文件
it('should call remove', () => {
const spySubscribable = spyOn(component, 'remove');
let index = 0;
let item = "org1";
let element = [{
"team": 'team1',
"orgsUpdated": ["org1", "org2"],
"removeFlag":false
}];
component.remove(item,element);
component.list1[index] = element;
component.list1[index].removeFlag = true;
expect(component.list1[index].removeFlag).toBeTruthy;
expect(spySubscribable).toHaveBeenCalled();
expect(component.list1[0]).toEqual(element);
});
问题是 spyOn(component, 'remove')
监视该方法以查看它被调用了多少次并删除了它的实现细节。要保留其实现细节,请在 spyOn
.
之后添加一个 .and.callThrough()
像这样:
it('should call remove', () => {
// change this line
const spySubscribable = spyOn(component, 'remove').and.callThrough();
let index = 0;
let item = "org1";
let element = [{
"team": 'team1',
"orgsUpdated": ["org1", "org2"],
"removeFlag":false
}];
// add this log and see what the value is and make sure
// this.list1.find returns something so it goes inside of the if
// block
console.log('List 1: ', component.list1);
component.remove(item,element);
component.list1[index] = element;
component.list1[index].removeFlag = true;
expect(component.list1[index].removeFlag).toBeTruthy;
expect(spySubscribable).toHaveBeenCalled();
expect(component.list1[0]).toEqual(element);
});
我正在尝试为以下方法编写测试用例。我写了类似下面的内容,但它并没有提高/更改代码覆盖率。有人可以帮助我做错什么吗?
组件
remove(item: string, element:any){
const found = this.list1.find(el => el.team == element.tirname);
if(found){
var index = this.list1.indexOf(found);
if (index >= 0) {
this.list1[index].removeFlag = true;
const index1= this.list1[index].orgsUpdated.indexOf(item);
if (index1 >= 0) {
this.list1[index]['orgsUpdated'].splice(index1, 1);
this.list1[index]['removeFlag'] = false;
}
}
}
const found1 = this.list2.find(el => el.name == element.name);
if(found1){
var index2 = this.list2.indexOf(found1);
if (index2 >= 0) {
const index1= this.list2[index2].gOrgNames.indexOf(item);
if (index1 >= 0) {
this.list2[index]['gitOrganizationNames'].splice(index1, 1);
}
}
}
}
规格文件
it('should call remove', () => {
const spySubscribable = spyOn(component, 'remove');
let index = 0;
let item = "org1";
let element = [{
"team": 'team1',
"orgsUpdated": ["org1", "org2"],
"removeFlag":false
}];
component.remove(item,element);
component.list1[index] = element;
component.list1[index].removeFlag = true;
expect(component.list1[index].removeFlag).toBeTruthy;
expect(spySubscribable).toHaveBeenCalled();
expect(component.list1[0]).toEqual(element);
});
问题是 spyOn(component, 'remove')
监视该方法以查看它被调用了多少次并删除了它的实现细节。要保留其实现细节,请在 spyOn
.
.and.callThrough()
像这样:
it('should call remove', () => {
// change this line
const spySubscribable = spyOn(component, 'remove').and.callThrough();
let index = 0;
let item = "org1";
let element = [{
"team": 'team1',
"orgsUpdated": ["org1", "org2"],
"removeFlag":false
}];
// add this log and see what the value is and make sure
// this.list1.find returns something so it goes inside of the if
// block
console.log('List 1: ', component.list1);
component.remove(item,element);
component.list1[index] = element;
component.list1[index].removeFlag = true;
expect(component.list1[index].removeFlag).toBeTruthy;
expect(spySubscribable).toHaveBeenCalled();
expect(component.list1[0]).toEqual(element);
});