The difference between assert.equal and assert.deepEqual in Javascript testing with Mocha?
我正在使用Mocha在Express.js应用程序中测试一个小模块。 在此模块中,我的函数之一返回一个数组。 我想测试数组对于给定的输入是否正确。 我这样做是这样的:
1 2 3 4 5 6 | suite('getWords', function(){ test("getWords should return list of numbers", function() { var result = ['555', '867', '5309']; assert.equal(result, getWords('555-867-5309')); }); }); |
运行此命令时,出现以下断言错误:
1 | AssertionError: ["555","867","5309"] == ["555","867","5309"] |
但是,当我将测试更改为
1 | [1,2,3] === [1,2,3] |
进入node.js命令行,我仍然会得到false。
为什么数组不比较其他值的方式(例如
Why do arrays not compare the way other values do (e.g. 1==1)
数字,字符串,布尔值,
另一方面,数组是对象。 在这种情况下,
what is the difference between assert.equal and assert.deepEqual?
另一方面,深度(aka结构)相等性不测试操作数是否是同一对象,而是测试它们是否等效。 从某种意义上讲,您可以说它迫使对象进行比较,就像它们是值一样。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | var a = [1,2,3] var b = a // As a and b both refer to the same object a == b // this is true a === b // and this is also true a = [1,2,3] // here a and b have equivalent contents, but do not b = [1,2,3] // refer to the same Array object. a == b // Thus this is false. assert.deepEqual(a, b) // However this passes, as while a and b are not the // same object, they are still arrays containing 1, 2, 3 assert.deepEqual(1, 1) // Also passes when given equal values var X = function() {} a = new X b = new X a == b // false, not the same object assert.deepEqual(a, b) // pass, both are unadorned X objects b.foo = 'bar' assert.deepEqual(a, b) // fail! |