Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jasmine.matchersUtil.equals vs===_Javascript_Selenium_Testing_Selenium Webdriver_Protractor - Fatal编程技术网

Javascript jasmine.matchersUtil.equals vs===

Javascript jasmine.matchersUtil.equals vs===,javascript,selenium,testing,selenium-webdriver,protractor,Javascript,Selenium,Testing,Selenium Webdriver,Protractor,我们已经开发了一套相当大的定制jasmine Matcher,这有助于使我们的代码更干净,避免代码重复。我注意到一些定制的jasmine Matcher使用了==平等测试和一些jasmine.matchersUtil.equals。例如: toHaveHandCursor: function() { return { compare: function(actual) { return { pass: actual.

我们已经开发了一套相当大的定制jasmine Matcher,这有助于使我们的代码更干净,避免代码重复。我注意到一些定制的jasmine Matcher使用了
==
平等测试和一些
jasmine.matchersUtil.equals
。例如:

toHaveHandCursor: function() {
    return {
        compare: function(actual) {
            return {
                pass: actual.getCssValue("cursor").then(function(cursor) {
                    return cursor === "pointer";
                })
            };
        }
    };
},

toBeActive: function() {
    return {
        compare: function(elm) {
            return {
                pass: protractor.promise.all([
                    elm.getId(),
                    browser.driver.switchTo().activeElement().getId()
                ]).then(helpers.spread(function (currentElementID, activeElementID) {
                    return jasmine.matchersUtil.equals(currentElementID, activeElementID);
                })),
                message: "Element is not active."
            };
        }
    };
}
问题:

jasmine.matchersUtil.equals
=
相等性测试之间有什么区别?应该首选哪种方法


换句话说,一般来说,如果我们只使用
===
,我们会有风险吗?

据我所知,我在
jasmine.matchersUtil.equals
==
中发现了一些东西:

根据定义,
==
根据其
值和
类型比较这两个实体。它是一个
严格的
比较运算符。例如:

2 === 2 //true
2 === 3 //false
2 === '2' //false
0 === -0 //true 

另一方面,
jasmine.matchersUtil.equals
是基于下划线J的
.isEqual
逻辑实现的,并基于一种逻辑测试是否应将传递给它的实体视为相等,即使它们的
类型不同。类似这样的事情-

jasmine.matchersUtil.equals(2, 2) //true
jasmine.matchersUtil.equals(2, 3) //false
jasmine.matchersUtil.equals(2, '2') //false
jasmine.matchersUtil.equals(0, -0) //false
这是git回购协议的摘录-

// Identical objects are equal. `0 === -0`, but they aren't identical.
if (a === b) { return a !== 0 || 1 / a == 1 / b; }
编辑:
jasmine.matchersUtil.equals()
的另一个优点是,我们实际上可以实现我们自己的自定义平等测试程序,这样就可以避免一些被认为会产生问题的场景。下面是一个自定义等式测试仪的示例,与以下示例一起使用-

var customTester = function(first, second) {
    return first == second;
};
少数情况-

  • 假设需要检查元素文本是否为空或是否有特定值,并且为其开发了自定义jasmine matcher,那么-

    "5" === 5 //if operation on elem returns "5" then custom matcher gives false
    jasmine.matchersUtil.equals("5", 5, customTester) //true when implemented with custom equality testers
    
    undefined === null //if operation on elem returns undefined then custom matcher gives false
    jasmine.matchersUtil.equals("", null, customTester) //true when implemented with custom equality testers
    
    NaN === NaN //if operation on elem returns NaN then custom matcher gives false
    jasmine.matchersUtil.equals(NaN, NaN) //true
    
  • 使用自定义匹配器更容易检查对象的相等性。例如:

    {name: 'hill'} === {name: 'hill'} //false
    jasmine.matchersUtil.equals({name: 'hill'}, {name: 'hill'}) //true
    
  • 每当我们知道可能会出现不同于预期值的值,或者出现上述任何情况时,我们总是使用自定义匹配器。如果期望值保证为单个值,则使用
    ==
    是有意义的。希望能有帮助