Chai Library中的equal和eql有什么区别 [英] What is the difference between equal and eql in Chai Library
问题描述
我对Javascript很新,我对Chai库进行单元测试有一个简单的问题。
I'm pretty new to Javascript, and I have a quick question regarding the Chai library for making unit tests.
当我在Chai上学习一些材料时库,我看到一个声明等于断言目标严格等于(===)到值和eql断言目标与值非常相等。。
When I was studying some materials on the Chai library, I saw a statement saying equal "Asserts that the target is strictly equal (===) to value" and eql "Asserts that the target is deeply equal to value.".
但我对于严格和深度之间的差异感到困惑。
But I'm confused about what the difference is between strictly and deeply.
推荐答案
严格相等(或 ===
)表示您正在将完全相同的对象与自身进行比较:
Strictly equal (or ===
) means that your are comparing exactly the same object to itself:
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
Deeply Equal 意味着比较对象的每个属性(以及可能的深层链接对象)具有相同的值。所以:
Deeply Equal on the other hand means that every property of the compared objects (and possible deep linked objects) have the same value. So:
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
这篇关于Chai Library中的equal和eql有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!