在 Chai 断言库中,当我们已经有了“assert.deepEqual()”时,“assert.equal()”有什么用"和 "assert.strictEqual()"用于严格和深度相等断言? 还提到“assert.equal()”使用非严格相等。非严格平等和深度平等有什么区别? 为什么我们不能只使用“deepEqual”而不是“equal”?
最佳答案
当您 checkout Chai documentation 时对于 .deep
函数,您会得到非常清晰的描述:
Causes all .equal, .include, .members, .keys, and .property assertions that follow in the chain to use deep equality instead of strict (===) equality.
当您使用equal
时,Chai 使用===
比较。因此,当比较对象时,Chai 将检查引用。
当您使用 deepEqual
时,Chai 将沿着对象层次结构向下并比较每个属性的每个值。
例子:
const a = {"a": "a"};
const b = {"a": "a"};
expect(a).to.equal(b); // false, as a refers to a different object than b
expect(a).to.deep.equal(b); // true, as the value of every property of a and b equals
关于javascript - 在 Chai 断言库中,当我们已经有了 "assert.equal()"和 "assert.deepEqual()"时, "assert.strictEqual()"有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63249848/