gpt4 book ai didi

angular - else单元测试中未采用的路径

转载 作者:行者123 更新时间:2023-12-03 16:54:55 26 4
gpt4 key购买 nike

我正在研究 Angular 6

I don't have any else statement in my below code.But I am unable to cover branches due to else path not taken .What I need to do to get 100% branch coverage in this case?


 getHeaderDocumentList(documents: any) {
if (documents) {
documents.result.docAttachment.forEach(element => {
this.headerdocumentdetails.push(element.DocumentUniqueID);
});
}
}

最佳答案

为了获得完整的覆盖率报告,代码最终需要命中else -path(此处不明确存在)。为此,传入falsy参数,例如0 | false | undefined | null | NaN | '' | "" | ``,如下所示:

component.getHeaderDocumentList(false);
expect(false).toEqual(false); // This line is optional. Most test suites require some kind of assertion in each test.

现在,您的测试套件应报告两个分支均已覆盖。

Last solution would be to add /* istanbul ignore else */ in before the if case. That will tell the coverage reporter to effectively ignore the else path

关于angular - else单元测试中未采用的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58877215/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com