在 ie8 中使用茉莉花比较数组失败

Using jasmine to compare arrays fails in ie8

本文关键字:比较 数组 失败 茉莉花 ie8      更新时间:2023-09-26

显然 ie8 有三个属性,这些属性通过调用 String.prototype.match() 附加到生成的数组中:

inputindexlastIndex

(MSDN 文档)

结果是,使用 Jasmine 的.toEqual()匹配器时,数组比较失败。

我仍在研究单元测试的学习曲线,所以我只是好奇处理这种失败的正确方法是什么。

以下有效,但似乎有点蹩脚:

 numArray = str.match(/'d+('.'d+)?/g);
 if (numArray && numArray.input) {
      delete numArray.index;
      delete numArray.input;
      delete numArray.lastIndex;
 }

Underscore的"差异"方法可以帮助 -

expect(_.difference(['item1', 'item2'], ['item1', 'item2'])).toEqual([]);

http://underscorejs.org/#difference

我认为@monkeyboy的答案是不正确的。

由于underscore.difference()返回第一个数组中不存在的元素:_.difference([1],[1,2]);也是[],因此测试将在不应该通过的时候通过。我找不到使用下划线解决此问题的方法。

所以我正在使用:

expect(JSON.stringify(result)).toBe(JSON.stringify(expected));

按预期工作。

无论如何,我想知道其他人是如何做到这一点的。