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

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

问题描述

我正在研究角度6

我的下面的代码中没有任何其他语句.但是由于未采用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-路径.为此,传入一个像0 | false | undefined | null | NaN | '' | "" | ``这样的falsy参数,如

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 */.这将告诉报道报道记者有效地忽略其他路径

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天全站免登陆