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

else path not taken in unit testing

我正在研究 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-路径。为此,传入一个 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

只要在没有else部分的if语句前加上/* istanbul ignore else */即可。报道记者将忽略其他路径。