[英]How do I cover the javascript code pattern || {}
I am using JSCover running my Jasmine SpecRunner, which inspects a code fragment of the sort: 我正在使用运行我的Jasmine SpecRunner的JSCover,它检查了以下代码片段:
var MyRootObject = MyRootObject || {};
My goal is to bring coverage up to 100%, but I am not sure how best to cover this method of initialising the MyRootObject. 我的目标是使覆盖率达到100%,但是我不确定如何最好地涵盖这种初始化MyRootObject的方法。
This pattern is used at the start of every library javascript file, to ensure that the order in which the files are loaded is unimportant (and the functions and variables inside are $.extend'ed). 每个库javascript文件的开头都使用此模式,以确保文件的加载顺序不重要(并且$ .extend'ed里面的函数和变量)。
The problem basically is that the first and only time this script is loaded in the SpecRunner, it runs only one side of this test, leaving the other path untested. 基本上,问题在于,第一次且唯一一次在SpecRunner中加载此脚本时,它仅运行此测试的一侧,而另一路径未经测试。
After digging around, I found a solution that works for me, of course, your mileage may vary: 深入研究后,我发现了一个适合我的解决方案,当然,您的工作量可能会有所不同:
var MyRootObject = MyRootObject || {};
$.extend(true, MyRootObject, {...});
I have replaced with: 我已替换为:
var MyRootObject = $.extend(true, MyRootObject, {...});
Internally inside JQuery, if the target (MyRootObject) is null or undefined, it automatically assigns the target inside the function to be {}. 在JQuery内部,如果目标(MyRootObject)为null或未定义,它将自动将函数内的目标分配为{}。 By using an external library (which I was already using), I no longer have a conditional path I am not following, as this is delegated to the JQuery library. 通过使用外部库(我已经在使用),我不再有我不遵循的条件路径,因为它被委托给了JQuery库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.