【发布时间】:2017-08-25 12:48:37
【问题描述】:
我有一个使用 mocha 和 chai 的期望创建的单元测试测试用例,我在其中深入比较了一组值对象与 JSON 文件的解析内容。
我的记录对象有大约 20 个属性,目前只有价格会导致不匹配。在 diff 中,我只看到其中的五个。
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 中显示整个对象。
如何配置 mocha 的差异显示?
这是一个展示问题的人为的元句法示例:
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 差异问题应该在一个属性的错误上显示整个差异:
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
Louis 修改 chai 的答案并不会变得更好,因为它只会先转储整个实际数组,然后显示无用的差异:
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 虽然chai is appearantly not taking care of the diff@Louis 没关系
标签: javascript typescript mocha.js diff chai