javascript Jasmine.js 比较数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15717844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jasmine.js comparing arrays
提问by user2032804
Is there a way in jasmine.js to check if two arrays are equal, for example:
jasmine.js 中有没有办法检查两个数组是否相等,例如:
arr = [1, 2, 3]
expect(arr).toBe([1, 2, 3])
expect(arr).toEqual([1, 2, 3])
Neither seems to work.
两者似乎都不起作用。
回答by TheEwook
Just did the test and it works with toEqual
刚刚做了测试,它适用于 toEqual
please find my test:
请找到我的测试:
describe('toEqual', function() {
it('passes if arrays are equal', function() {
var arr = [1, 2, 3];
expect(arr).toEqual([1, 2, 3]);
});
});
Just for information:
仅供参考:
toBe() versus toEqual(): toEqual() checks equivalence. toBe(), on the other hand, makes sure that they're the exact same object.
toBe() 与 toEqual():toEqual() 检查等价性。另一方面,toBe() 确保它们是完全相同的对象。
回答by Utkarsh Joshi
You can compare an array like the below mentioned if the array has some values
如果数组有一些值,您可以比较如下所述的数组
it('should check if the array are equal', function() {
var mockArr = [1, 2, 3];
expect(mockArr ).toEqual([1, 2, 3]);
});
But if the array that is returned from some function has more than 1 elements and all are zero then verify by using
但是,如果从某个函数返回的数组有 1 个以上的元素并且都为零,则通过使用验证
expect(mockArray[0]).toBe(0);
回答by Ohad Sadan
just for the record you can always compare using JSON.stringify
只是为了记录,您始终可以使用 JSON.stringify 进行比较
const arr = [1,2,3];
expect(JSON.stringify(arr)).toBe(JSON.stringify([1,2,3]));
expect(JSON.stringify(arr)).toEqual(JSON.stringify([1,2,3]));
const arr = [1,2,3];
expect(JSON.stringify(arr)).toBe(JSON.stringify([1,2,3]));
expect(JSON.stringify(arr)).toEqual(JSON.stringify([1,2,3]));
It's all meter of taste, this will also work for complex literal objects
这都是品味,这也适用于复杂的文字对象
回答by antimatter
I had a similar issue where one of the arrays was modified. I was using it for $httpBackend
, and the returned object from that was actually a $promise
object containing the array (not an Array
object).
我有一个类似的问题,其中一个数组被修改。我将它用于$httpBackend
,从中返回的对象实际上是一个$promise
包含数组的Array
对象(不是对象)。
You can create a jasmine matcher to match the array by creating a toBeArray
function:
您可以通过创建一个toBeArray
函数来创建一个茉莉花匹配器来匹配数组:
beforeEach(function() {
'use strict';
this.addMatchers({
toBeArray: function(array) {
this.message = function() {
return "Expected " + angular.mock.dump(this.actual) + " to be array " + angular.mock.dump(array) + ".";
};
var arraysAreSame = function(x, y) {
var arraysAreSame = true;
for(var i; i < x.length; i++)
if(x[i] !== y[i])
arraysAreSame = false;
return arraysAreSame;
};
return arraysAreSame(this.actual, array);
}
});
});
And then just use it in your tests like the other jasmine matchers:
然后像其他茉莉花匹配器一样在您的测试中使用它:
it('should compare arrays properly', function() {
var array1, array2;
/* . . . */
expect(array1[0]).toBe(array2[0]);
expect(array1).toBeArray(array2);
});