期望数组是数组

时间:2016-02-03 02:22:08

标签: arrays node.js unit-testing should.js

编写一些测试并遇到错误。阵列看起来和我一样,但显然不是。这是我得到的错误。关于如何解决它的任何想法?

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

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);


});

2 个答案:

答案 0 :(得分:2)

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. 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. e.g.

// 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))

答案 1 :(得分:0)

Travis's answer之外。 Should.js还提供了.eql and .deepEqual断言来测试深度相等:

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