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

时间:2019-11-15 12:36:12

标签: angular typescript unit-testing jasmine karma-coverage

我正在研究角度6

  

我的下面的代码中没有任何其他语句。但是由于未采用else路径,所以我无法覆盖分支。在这种情况下,我需要怎么做才能获得100%的分支覆盖率?

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

1 个答案:

答案 0 :(得分:1)

为了获得完整的覆盖范围报告,代码最终需要命中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.

现在,测试人员应该报告两个分支都已覆盖。