jasmine.matchersUtil.equals vs ===

ale*_*cxe 13 javascript testing selenium selenium-webdriver protractor

我们开发了一套相当大的自定义茉莉花匹配器,有助于使我们的代码更清晰,避免代码重复.我注意到一些自定义茉莉花匹配器使用了===相等测试和一些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."
            };
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

问题:

jasmine.matchersUtil.equals===平等测试有什么区别,哪种方法应该首选?

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

Gir*_*tur 7

据我所了解,这里是我发现一些事情jasmine.matchersUtil.equals===:

根据定义,===比较两个实体的基础valuetype.它是一个strict比较运算符.例如:

2 === 2 //true
2 === 3 //false
2 === '2' //false
0 === -0 //true 
Run Code Online (Sandbox Code Playgroud)

(可以出现+ 0,0和-0的示例场景)

另一方面,jasmine.matchersUtil.equals基于_.isEqual下划线的逻辑和基于逻辑的测试来实现,该逻辑确定传递给它的实体是否应该被认为是相等的,即使它们types是不同的.像这样的东西 -

jasmine.matchersUtil.equals(2, 2) //true
jasmine.matchersUtil.equals(2, 3) //false
jasmine.matchersUtil.equals(2, '2') //false
jasmine.matchersUtil.equals(0, -0) //false
Run Code Online (Sandbox Code Playgroud)

这是git repo的摘录 -

// Identical objects are equal. `0 === -0`, but they aren't identical.
if (a === b) { return a !== 0 || 1 / a == 1 / b; }
Run Code Online (Sandbox Code Playgroud)

编辑: 增加的优点jasmine.matchersUtil.equals()是我们可以实际实现我们自己的自定义相等测试器,以便可以避免几个被认为会产生问题的场景.以下是与以下示例一起使用的自定义等式测试程序示例 -

var customTester = function(first, second) {
    return first == second;
};
Run Code Online (Sandbox Code Playgroud)

几个场景 -

  1. 假设是否需要检查元素文本是否为空或具有某些特定值,并且为其开发了自定义jasmine匹配器,则 -

    "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
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用自定义匹配器可以更轻松地检查对象的相等性.例如:

    {name: 'hill'} === {name: 'hill'} //false
    jasmine.matchersUtil.equals({name: 'hill'}, {name: 'hill'}) //true
    
    Run Code Online (Sandbox Code Playgroud)
  3. 使用自定义匹配器检查元素返回值是否与正则表达式相等更容易,而不是使用逻辑实现逻辑===.

  4. 构造函数的比较更容易.

  5. 如果需要通过将错误对象传递给自定义匹配器来测试错误,则会处理错误验证.

每当我们知道除了可能出现的预期值之外可能存在不同的值,或者任何上述条件可能发生时,我们总是使用自定义匹配器.如果期望保证是单个值,那么使用===是有意义的.希望能帮助到你.