将两个对象数组与其值进行比较,单元测试

时间:2017-09-18 14:49:35

标签: javascript arrays unit-testing ecmascript-6 mocha

假设我有两个相似的数组,如何编写unittest来检查这些数组是否具有相同的值。而且这些价值是相等的。我用mocha和酶进行单元测试。比较这个值的最佳方法是什么?

const devices = [{
        deviceType: 'Tag',
        deviceId: 1,
        name: 'Tag For Sending and alarm',
        version: '',
        location: 'Room1',
        lastAliveMessage: '',
        deviceStatus: 'Active',
        actions: 'offline',

    },{
        deviceType: 'Tag1',
        deviceId: 2,
        name: 'Tag For Sending and alarm2 ',
        version: '',
        location: 'Room2',
        lastAliveMessage: '',
        deviceStatus: 'Active',
        actions: 'offline',
    }]

const devices2 = [{
        deviceType: 'Tag',
        deviceId: 1,
        name: 'Tag For Sending and alarm',
        version: '',
        location: 'Room1',
        lastAliveMessage: '',
        deviceStatus: 'Active',
        actions: 'offline',

    },{
        deviceType: 'Tag1',
        deviceId: 2,
        name: 'Tag For Sending and alarm2 ',
        version: '',
        location: 'Room2',
        lastAliveMessage: '',
        deviceStatus: 'Active',
        actions: 'offline',
    }]

 data.forEach(item => {
                devices.forEach(device =>{
item.deviceId.should.be.equal(device.deviceId))
                })
            })

2 个答案:

答案 0 :(得分:0)

devices.forEach((device, index) => {  
     data[index].deviceType.props.children.should.be.equal(device.deviceType)
     data[index].deviceId.props.children.should.be.equal(device.deviceId)
        })

答案 1 :(得分:0)

如果使用Chai,则可以使用deep.equal检查对象的数组是否相同。即使对象中键的顺序在两者之间不相同,它也将起作用。

expect(devices).to.deep.equal(devices2)