为什么下面的会失败?

expect([0,0]).to.equal([0,0]);

正确的测试方法是什么呢?


当前回答

对于无序深度相等,使用成员

期望([1,2,3]).to.have.members ((3,2,1));/ /传递期望([1,2,3]).to.have.members ([1, 2, 3]);//传递expect([1,2,3]).to.eql([3,2,1]);/ /失败

其他回答

你可以使用.deepEqual()

const { assert } = require('chai');

assert.deepEqual([0,0], [0,0]);
import chai from 'chai';
const arr1 = [2, 1];
const arr2 = [2, 1];
chai.expect(arr1).to.eql(arr2); // Will pass. `eql` is data compare instead of object compare.

对于expect, .equal将比较对象而不是它们的数据,在您的示例中,它是两个不同的数组。

使用.eql来深度比较值。查看这个链接。 或者你可以使用.deep.equal来模拟。eql。 或者在你的情况下,你可能想检查。members。

对于断言,您可以使用. deepequal, link。

对于无序深度相等,使用成员

期望([1,2,3]).to.have.members ((3,2,1));/ /传递期望([1,2,3]).to.have.members ([1, 2, 3]);//传递expect([1,2,3]).to.eql([3,2,1]);/ /失败

尝试使用深度等号。它将比较嵌套数组和嵌套Json。

expect({ foo: 'bar' }).to.deep.equal({ foo: 'bar' });

请参考主要文档网站。