2017-08-25 69 views
3

我有一个使用mocha和chai创建的单元测试测试用例,我将深度比较value对象数组与JSON文件的解析内容。如何让mocha在断言错误中显示整个对象?

我的记录对象有大约20个属性,目前只有价格可能会导致不匹配。在差异上,我只看到其中五个。

expect(records).to.deep.equal(expected); 

     "data": { 
    -  "price": 3578 
    +  "price": 3438 
     "not_important": "foo" 
     "also_not_important": "bar" 
     } 
     "data": { 
    -  "price": 1828 
    +  "price": 1698 
     "not_important": "foo" 
     "also_not_important": "bar" 
     } 

这是在大多数情况下有用的默认,但在这一次它混淆了哪些具体数据对象被打破断言,因为我只看到冗余数据在这里。

假设有在数据对象的important属性,将使其很清楚什么期望是打破了测试。因此,我希望能够配置显示的属性或在diff中显示整个对象。

如何配置摩卡的差异显示?


这里是一个人为的元语法示例展示了此问题:

import {expect} from "chai"; 

describe(("diff problem"),() => { 
    it("should show case that the diff is not shown properly",() => { 
     const actual = { 
      a: 1, 
      troiz: 0, 
      bar: 0, 
      baz: 2, 
      poit: 3, 
      narf: 4, 
      fizzbuzz: 117, 
      fizz: 5, 
      buzz: 4, 
      waldo: 115, 
      mos: 85465, 
      important: "THIS IS IMPORTANT", 
     }; 

     const expected = { 
      ...actual, 
      a: 0, 
     }; 

     return expect(actual).to.deep.equal(expected); 
    }); 
}); 

该测试用例的输出将是:

2)SourceParser差异问题应显示上的错误整个DIFF一个属性:

AssertionError: expected { Object (a, troiz, ...) } to deeply equal { Object (a, troiz, ...) } 
    + expected - actual 

    { 
    - "a": 1 
    + "a": 0 
    "bar": 0 
    "baz": 2 
    "buzz": 4 
    "fizz": 5 

然而,它将有助于看到:important: "THIS IS IMPORTANT"也是如此。


这里是用于阵列情况下的变形例:

describe(("diff problem with an array"),() => { 
    it("should show case that the diff is not shown properly for deep equal of arrays",() => { 
     const anEntity = { 
      a: 1, 
      troiz: 0, 
      bar: 0, 
      baz: 2, 
      poit: 3, 
      narf: 4, 
      fizzbuzz: 117, 
      fizz: 5, 
      buzz: 4, 
      waldo: 115, 
      mos: 85465, 
      important: "IMPORTANT", // assume that each item has a unique important property, which is why it's helpful for it to be shown 
     }; 

     const offendingItem = { 
      ...anEntity, 
      a: 0, 
     }; 

     const actual = [ 
      anEntity, 
      offendingItem, 
      anEntity, 
     ]; 

     const expected = [ 
      anEntity, 
      anEntity, 
      anEntity, 
     ]; 

     return expect(actual).to.deep.equal(expected); 
    }); 

的输出将是:

AssertionError: expected [ Array(3) ] to deeply equal [ Array(3) ] 
    + expected - actual 

     "troiz": 0 
     "waldo": 115 
    } 
    { 
    - "a": 0 
    + "a": 1 
     "bar": 0 
     "baz": 2 
     "buzz": 4 
     "fizz": 5 

,它不会得到更好地与路易斯的答案改性如柴它仅转储整个实际阵列的第一,然后示出了非有用DIFF:

AssertionError: expected [ { a: 1, 
    troiz: 0, 
    bar: 0, 
    baz: 2, 
    poit: 3, 
    narf: 4, 
    fizzbuzz: 117, 
    fizz: 5, 
    buzz: 4, 
    waldo: 115, 
    mos: 85465, 
    important: 'IMPORTANT' }, 
    { a: 0, 
    troiz: 0, 
    bar: 0, 
    baz: 2, 
    poit: 3, 
    narf: 4, 
    fizzbuzz: 117, 
    fizz: 5, 
    buzz: 4, 
    waldo: 115, 
    mos: 85465, 
    important: 'IMPORTANT' }, 
    { a: 1, 
    troiz: 0, 
    bar: 0, 
    baz: 2, 
    poit: 3, 
    narf: 4, 
    fizzbuzz: 117, 
    fizz: 5, 
    buzz: 4, 
    waldo: 115, 
    mos: 85465, 
    important: 'IMPORTANT' } ] to deeply equal [ { a: 1, 
    troiz: 0, 
    bar: 0, 
    baz: 2, 
    poit: 3, 
    narf: 4, 
    fizzbuzz: 117, 
    fizz: 5, 
    buzz: 4, 
    waldo: 115, 
    mos: 85465, 
    important: 'IMPORTANT' }, 
    { a: 1, 
    troiz: 0, 
    bar: 0, 
    baz: 2, 
    poit: 3, 
    narf: 4, 
    fizzbuzz: 117, 
    fizz: 5, 
    buzz: 4, 
    waldo: 115, 
    mos: 85465, 
    important: 'IMPORTANT' }, 
    { a: 1, 
    troiz: 0, 
    bar: 0, 
    baz: 2, 
    poit: 3, 
    narf: 4, 
    fizzbuzz: 117, 
    fizz: 5, 
    buzz: 4, 
    waldo: 115, 
    mos: 85465, 
    important: 'IMPORTANT' } ] 
     + expected - actual 

      "troiz": 0 
      "waldo": 115 
     } 
     { 
     - "a": 0 
     + "a": 1 
      "bar": 0 
      "baz": 2 
      "buzz": 4 
      "fizz": 5 
+0

@Louis它应该没关系,虽然[柴显然没有照顾差异](https://github.com/chaijs/chai/issues/469#issuecomment-153759558) – k0pernikus

回答

2

据我所知,没有内置的方式来获得柴或摩卡产生差异列表,将添加到由DIFF提供的上下文某些字段不在负责的测试失败。而且我不知道任何扩展程序会完全符合您的要求。所以我只知道解决方法。


如果设置truncateThreshold配置设置为较大的值,或0,如果你不希望任何截断,在出现前的差异将显示整个对象的失败消息。所以,如果我添加到您的代码:

chai.config.truncateThreshold = 0; // 0 means "don't truncate, ever". 

(配置选项都覆盖在这个documentation page。)

然后我得到的错误是:

 AssertionError: expected { a: 1, 
    troiz: 0, 
    bar: 0, 
    baz: 2, 
    poit: 3, 
    narf: 4, 
    fizzbuzz: 117, 
    fizz: 5, 
    buzz: 4, 
    waldo: 115, 
    mos: 85465, 
    important: 'THIS IS IMPORTANT' } to deeply equal { a: 0, 
    troiz: 0, 
    bar: 0, 
    baz: 2, 
    poit: 3, 
    narf: 4, 
    fizzbuzz: 117, 
    fizz: 5, 
    buzz: 4, 
    waldo: 115, 
    mos: 85465, 
    important: 'THIS IS IMPORTANT' } 
     + expected - actual 

     { 
     - "a": 1 
     + "a": 0 
     "bar": 0 
     "baz": 2 
     "buzz": 4 
     "fizz": 5 

此外,一种方式来获得错误消息的自定义信息是设置的自定义消息和断言,比如:

expect(actual).to.deep.equal(
      expected, 
      `failed equality test on object with important field set to: ${actual.important}`) 

自定义消息可以像您需要的那样冗长或简洁。

如果只有一个对您而言很重要的区域用于区分对象,则自定义消息可能足以获取跟踪问题所需的信息。


对于对象的阵列,则可以通过经由actualexpected阵列迭代和每个成员进行比较执行比较。它将基本上像上面描述的那样工作。当然,这有缺点:例如,如果第1项和第10项不同,则只会得到第1项的报告,因为测试将以比较失败结束。当你解决这个问题并再次运行测试时,你会得到一个项目10的报告。无论在实践中这是一个主要问题取决于你正在测试的数据类型。


有一件事我一直在默认比较算法没有做我想要的东西的情况下做的是导入我配置我喜欢的一个版本比较库,然后使用该库对象之间进行的diff那我关心并将结果合并成最终报告,然后用断言进行检查。

再说一遍,我不知道一个库,它会做特别是你在找什么。不过,我可以想象通过actualexpected数组迭代产生一个差异报告,然后将它们组合成包含识别信息的较大报告。

+0

我在想一个自定义消息,然后问题是我深深地比较了两个数据对象的数组。 – k0pernikus

+0

而'truncateThreshold'也不是一个有用的设置,因为这只会输出整个实际的数组。它不会影响差异。我将修改阵列问题的示例。 – k0pernikus

+0

这个答案不解决数组比较的情况。绝对值得赞扬:) – k0pernikus