Chai Library中的equal和eql有什么区别

时间:2016-04-22 16:29:18

标签: javascript unit-testing chai

我对Javascript很陌生,我对Chai库进行单元测试有一个简单的问题。

当我在Chai图书馆学习一些材料时,我看到一个声明说“等于”的声明:断言目标严格等于(===)到值"和eql"断言目标与价值深度相等。"。

但我对于严格和深刻之间的差异感到困惑。

1 个答案:

答案 0 :(得分:15)

严格相等(或===)表示您正在将完全相同的对象与自身进行比较:

var myObj = {
   testProperty: 'testValue'
};
var anotherReference = myObj;

expect(myObj).to.equal(anotherReference); // The same object, only referenced by another variable
expect(myObj).to.not.equal({testProperty: 'testValue'}); // Even though it has the same property and value, it is not exactly the same object
另一方面,

深度等于意味着比较对象的每个属性(以及可能的深层链接对象)具有相同的值。所以:

var myObject = {
    testProperty: 'testValue',
    deepObj: {
        deepTestProperty: 'deepTestValue'
    }
}
var anotherObject = {
    testProperty: 'testValue',
    deepObj: {
        deepTestProperty: 'deepTestValue'
    }
}
var myOtherReference = myObject;

expect(myObject).to.eql(anotherObject); // is true as all properties are the same, even the inner object (deep) one
expect(myObject).to.eql(myOtherReference) // is still also true for the same reason
相关问题