6

Chai,匹配器是否与 rspecs 等效=~(这意味着具有所有元素,但顺序无关紧要。

传递示例

[1, 2, 3].should =~ [2, 1, 3]

失败

[1, 2, 3].should =~ [1, 2]
4

3 回答 3

10

您可以使用members最新 Chai 版本中提供的测试:

expect([4, 2]).to.have.members([2, 4]);
expect([5, 2]).to.not.have.members([5, 2, 1]);
于 2013-06-14T15:11:19.953 回答
5

我认为没有,但您可以通过构建一个助手轻松创建一个:

var chai = require('chai'),
    expect = chai.expect,
    assert = chai.assert,
    Assertion = chai.Assertion

Assertion.addMethod('equalAsSets', function (otherArray) {
    var array = this._obj;

    expect(array).to.be.an.instanceOf(Array);
    expect(otherArray).to.be.an.instanceOf(Array);

    var diff = array.filter(function(i) {return !(otherArray.indexOf(i) > -1);});

    this.assert(
        diff.length === 0,
        "expected #{this} to be equal to #{exp} (as sets, i.e. no order)",
        array,
        otherArray
    );
});

expect([1,2,3]).to.be.equalAsSets([1,3,2]);
expect([1,2,3]).to.be.equalAsSets([3,2]);


flag

请注意,这不是无序相等测试,而是设置相等。任一数组中都允许重复项;这通过了,例如:expect([1,2,3]).to.be.equalAsSets([1,3,2,2]);

于 2012-07-18T23:05:31.440 回答
0

http://www.rubydoc.info/gems/rspec-expectations/frames#Collection_membership

expect([4, 2]).to match_array([2, 4])
于 2015-02-04T07:50:29.083 回答