else 单元测试中未采用的路径 [英] else path not taken in unit testing

查看:15
本文介绍了else 单元测试中未采用的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 Angular 6

I am working on angular 6

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

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 |假 |未定义 |空 |钠 |'' |"" |``

In order to get full coverage reported, the code needs to eventually hit the (here non-explicitly-existent) else-path. For that matter pass in a falsy parameter like 0 | false | undefined | null | NaN | '' | "" | `` as in

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

现在您的测试套件应该报告两个分支都已覆盖.

Now your test-suite should report both branches as covered.

最后的解决方案是在 if 案例之前添加 /* istanbul ignore else */ .这将告诉记者有效地忽略 else 路径

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 单元测试中未采用的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆