显然 ie8 有三个属性,这些属性通过调用 String.prototype.match()
附加到生成的数组中:
input
、index
和lastIndex
(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));
按预期工作。
无论如何,我想知道其他人是如何做到这一点的。