【发布时间】:2020-12-07 20:16:19
【问题描述】:
我正在尝试提高 Angular 应用程序的代码覆盖率。在代码覆盖率中提到了 if else 条件没有被覆盖。谁能告诉我该怎么做? 欢迎询问更多代码详情。
public searchByText(textVal: any): void {
let matchedEquipments = [];
// **
if (this.model.searchText.length > 1) {
matchedEquipments = this.refineByText(textVal, this.equipments);
} else {
matchedEquipments = this.equipments;
}
// **
matchedEquipments = this.refineByPlant(this.model.plants, matchedEquipments);
matchedEquipments = this.refineByPlantIsland(this.model.plantIslands, matchedEquipments);
matchedEquipments = this.refineByProcess(this.model.processes, matchedEquipments);
matchedEquipments = this.refineByIndustry(this.model.divisions, matchedEquipments);
this.displayClientData(matchedEquipments);
this.updateSearchCounters(SelectionFilter.FreeText);
}
规格:
it('verify the result with search', async(() => {
equipmentSelectionComponent.searchByText("123");
//equipmentSelectionComponent.model.searchText = "123";
expect(equipmentSelectionComponent.matchedData.length).toBeGreaterThan(0);
}));
【问题讨论】:
-
.searchByText("")? -
它是组件的方法名
-
不,我的意思是你可以这样测试另一种情况。查看条件,考虑通过您的逻辑的可能路线。
-
我可以写一个条件成功的案例,另一个案例吗?
-
是的,您应该为每个案例至少进行一次测试。
标签: angular typescript unit-testing jasmine karma-runner