使用 Chai 断言比较两个对象
Compare two objects using Chai assertions
我有两个混合订单数组
const actualResult = [
{
start: '30',
end: '50',
locations: ['loc1', 'loc2'],
},
{
start: '20',
end: '40',
locations: ['loc3', 'loc4'],
},
];
const expectedResult = [
{
start: '20',
end: '40',
locations: ['loc4', 'loc3'],
},
{
start: '30',
end: '50',
locations: ['loc2', 'loc1'],
},
];
我写了下面的代码来使用 chai 断言比较它们
describe('test1', function () {
it('expect test', function () {
expect(actualResult).to.have.length(expectedResult.length);
for (b of expectedResult) {
var found = false;
for (d of actualResult) {
if (b.start == d.start && b.end == d.end) {
expect(b.locations).to.have.deep.members(d.locations);
found = true;
}
}
expect(found).to.be.true;
}
});
});
效果很好,但我想知道是否有任何直接的 chai 断言可以在一行中执行,例如
expect(actualResult).to.have.all.nested.members(expectedResult);
或更好的建议。
您可以使用 deep-equal-in-any-order 插件。
Chai plugin to match objects and arrays deep equality with arrays (including nested ones) being in any order.
It works in a similar way as deep.equal
but it doesn’t check the order of the arrays (at any level of nested objects and arrays). The array elements can be any JS entity (boolean, null, number, string, object, array…).
例如
const deepEqualInAnyOrder = require('deep-equal-in-any-order');
const chai = require('chai');
chai.use(deepEqualInAnyOrder);
const { expect } = chai;
describe('', () => {
it('should pass', () => {
const actualResult = [
{
start: '30',
end: '50',
locations: ['loc1', 'loc2'],
},
{
start: '20',
end: '40',
locations: ['loc3', 'loc4'],
},
];
const expectedResult = [
{
start: '20',
end: '40',
locations: ['loc4', 'loc3'],
},
{
start: '30',
end: '50',
locations: ['loc2', 'loc1'],
},
];
expect(actualResult).to.deep.equalInAnyOrder(expectedResult);
});
});
测试结果:
59308311
✓ should pass
1 passing (23ms)
我有两个混合订单数组
const actualResult = [
{
start: '30',
end: '50',
locations: ['loc1', 'loc2'],
},
{
start: '20',
end: '40',
locations: ['loc3', 'loc4'],
},
];
const expectedResult = [
{
start: '20',
end: '40',
locations: ['loc4', 'loc3'],
},
{
start: '30',
end: '50',
locations: ['loc2', 'loc1'],
},
];
我写了下面的代码来使用 chai 断言比较它们
describe('test1', function () {
it('expect test', function () {
expect(actualResult).to.have.length(expectedResult.length);
for (b of expectedResult) {
var found = false;
for (d of actualResult) {
if (b.start == d.start && b.end == d.end) {
expect(b.locations).to.have.deep.members(d.locations);
found = true;
}
}
expect(found).to.be.true;
}
});
});
效果很好,但我想知道是否有任何直接的 chai 断言可以在一行中执行,例如
expect(actualResult).to.have.all.nested.members(expectedResult);
或更好的建议。
您可以使用 deep-equal-in-any-order 插件。
Chai plugin to match objects and arrays deep equality with arrays (including nested ones) being in any order.
It works in a similar way as
deep.equal
but it doesn’t check the order of the arrays (at any level of nested objects and arrays). The array elements can be any JS entity (boolean, null, number, string, object, array…).
例如
const deepEqualInAnyOrder = require('deep-equal-in-any-order');
const chai = require('chai');
chai.use(deepEqualInAnyOrder);
const { expect } = chai;
describe('', () => {
it('should pass', () => {
const actualResult = [
{
start: '30',
end: '50',
locations: ['loc1', 'loc2'],
},
{
start: '20',
end: '40',
locations: ['loc3', 'loc4'],
},
];
const expectedResult = [
{
start: '20',
end: '40',
locations: ['loc4', 'loc3'],
},
{
start: '30',
end: '50',
locations: ['loc2', 'loc1'],
},
];
expect(actualResult).to.deep.equalInAnyOrder(expectedResult);
});
});
测试结果:
59308311
✓ should pass
1 passing (23ms)