简体   繁体   English

在QUnit中比较两个数组的最简单方法是什么

[英]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.

相关问题 以这种特定方式比较真实性的最简单方法是什么? - What is the simplest way to compare truthiness in this specific way? 比较两个数组的有效方法 - Efficient way to compare two arrays 如何在qunit测试中比较两件事? - How to compare two things in qunit test? QUnit-如何比较两个复杂的对象 - QUnit - how to compare two complex objects 在 JavaScript 中找到包含至少一个元素的最小子数组的最简单方法是什么? - What is the simplest way to find smallest subarray containing at least one element from each of the other arrays in JavaScript? 以秒为单位为 Tone.js 提供音符和持续时间数组的最简单方法是什么? - What is the simplest way of giving Tone.js arrays of notes and durations in seconds to play back? 比较两个字符串数组Javascript的最快/最有效的方法 - Fastest / most efficient way to compare two string arrays Javascript 有没有办法比较两个数字中的相应整数而不将它们转换为数组 - Is there a way to compare the corresponding integers in two numbers without converting them to arrays 如何比较这两个 arrays 使用 GAS 构建的方式? - Hwo to compare these two arrays the way they are built using GAS? 比较两个json数组并更改位置的最佳方法 - Best way to compare two json arrays and make changes to the position
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM