我有一个使用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
@Louis它应该没关系,虽然[柴显然没有照顾差异](https://github.com/chaijs/chai/issues/469#issuecomment-153759558) – k0pernikus