【问题标题】:Why does ava fail comparing list of objects and list of object literal?为什么 ava 比较对象列表和对象文字列表失败?
【发布时间】:2016-09-16 15:01:19
【问题描述】:

我正在使用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"}]                   

问题

我该如何解决这个问题?

【问题讨论】:

    标签: javascript unit-testing assertions ava deepequals


    【解决方案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);
    });
    

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

    【讨论】:

      猜你喜欢
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多