[英]What is the simplest way to compare two arrays in QUnit
I'm writing JavaScript unit tests (with the QUnit library). 我正在编写JavaScript单元测试(使用QUnit库)。 I need to verify that my array contains expected (and only) elements.
我需要验证我的数组包含预期(和唯一)元素。
var array = getArrayFunction(a, b);
equal(["one", "two", "three"], array, "Test is failing even if 'array' contains needed elements");
What would be the easiest way to do this? 最简单的方法是什么?
You should use deepEqual()
in lieu of equal()
. 你应该使用
deepEqual()
代替equal()
。 That will compare array elements and object properties, not just use the ==
comparison operator, which evaluates to false
for objects that don't share the same constructor. 这将比较数组元素和对象属性,而不仅仅是使用
==
比较运算符,对于不共享相同构造函数的对象,该运算符的计算结果为false
。
Docs here: https://api.qunitjs.com/deepEqual/ 文档: https : //api.qunitjs.com/deepEqual/
More information about JavaScript equality comparison: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness 有关JavaScript相等性比较的更多信息: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.