为什么ava无法比较对象列表和对象文字列表?

时间:2016-09-16 15:01:20

标签: javascript unit-testing assertions ava deepequals

我使用deepEqual断言,但我的测试失败了

测试

test('should return list of printers', t => {
    const clipboard = filter.asClipboardContent(scan);

    t.is(clipboard, [
        {hostname: '10.0.1.1', port: '9100', description: 'HP 5020-NL'},
        {hostname: '10.0.1.8', port: '9100', description: 'Brother 4002'}
    ]);
}

输出失败

 t.deepEqual(clipboard, [{ hostname: '10.0.1.1', port: '9100', description: 'HP 5020-NL' }, { hostname: '10.0.1.8', port: '9100', description: 'Brother 4002' }])
              |                                                                                                                                                   
              [Object{hostname:"10.0.1.1",port:9100,description:"HP 5020-NL"},Object{hostname:"10.0.1.8",port:9100,description:"Brother 4002"}]                   

问题

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我遇到了类型问题,port值一边是string而另一边是integer ...

test('should return list of printers', t => {
    const expected = [
        {hostname: '10.0.1.1', port: 9100, description: 'HP 5020-NL'},
        {hostname: '10.0.1.8', port: 9100, description: 'Brother 4002'}
    ];

    const clipboard = filter.asClipboardContent(scan);

    t.deepEqual(clipboard, expected);
});

在这种情况下,两个对象都是相同的。