简体   繁体   English

期望数组是数组

[英]Expect array to be array

Writing some tests and ran across an error. 编写一些测试并遇到错误。 The array's seem the same to me, but apparently not. 该数组对我来说似乎相同,但显然不一样。 This is the error I'm getting. 这是我得到的错误。 Any idea on what to do to fix it? 有什么解决办法的想法吗?

Expected Array [ 'A2T1511300361', 'A2T1511300362' ] to be Array [ 'A2T1511300361', 'A2T1511300362' ]

test.js test.js

var should = require('should'), 
io = require('socket.io-client'),
path = require('path'),
express = require(path.resolve('./config/lib/express')),
mongoose = require('mongoose'),
sinon = require('sinon')   

...

client.on('printerList', function(list){

    var tempArray = [];
    tempArray.push('A2T1511300361');
    tempArray.push('A2T1511300362');
    console.log(tempArray);

    list.should.equal(tempArray);


});

You cannot directly test array quality in the manner that you are doing. 您无法直接按照自己的方式测试阵列质量。 [1,2] and [1,2] may have the same elements, but they are different arrays. [1,2][1,2]可以具有相同的元素,但是它们是不同的数组。 More formally: 更正式地:

[ ] !== [ ] [ ] != [ ]

You are trying to test deep equality. 您正在尝试测试深度平等。 To do this, you need to check each array element. 为此,您需要检查每个数组元素。 Many methods in lodash , for example, can help you with this. 例如, lodash中的许多方法都可以帮助您。 eg 例如

// this uses ES6 syntax const _ = require('lodash') const arr1 = [1, 2] const arr2 = [1, 2] assert.equal(_.intersection(arr1, arr2).length, arr1.length)) assert.equal(_.intersection(arr1, arr2).length, arr2.length))

In addition to Travis's answer . 除了Travis的答案 Should.js also provides .eql and .deepEqual assertions which test for deep equality: Should.js还提供.eql和.deepEqual断言,用于测试深度相等性:

var expectedArray = [1, 2, 3];
var returnedArray = [1, 2, 3];
returnedArray.should.deepEqual(expectedArray);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM