Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对于Chai中的数组,等效于rspec=~_Javascript_Testing_Mocha.js_Chai - Fatal编程技术网

Javascript 对于Chai中的数组,等效于rspec=~

Javascript 对于Chai中的数组,等效于rspec=~,javascript,testing,mocha.js,chai,Javascript,Testing,Mocha.js,Chai,Chai,matchers是否与rspecs=~(这意味着拥有所有元素,但顺序并不重要)具有同等效力 例行公事 弱点 我认为没有,但您可以通过以下方式轻松创建: 请注意这不是无序相等测试,而是设置相等。两个数组中都允许重复项;这通过了,例如:expect([1,2,3]).to.Be.equalAsSets([1,3,2,2]);您可以使用最新版本中提供的成员测试: expect([4, 2]).to.have.members([2, 4]); expect([5, 2]).to.not.hav

Chai,matchers是否与rspecs
=~
(这意味着拥有所有元素,但顺序并不重要)具有同等效力

例行公事 弱点
我认为没有,但您可以通过以下方式轻松创建:


请注意这不是无序相等测试,而是设置相等。两个数组中都允许重复项;这通过了,例如:
expect([1,2,3]).to.Be.equalAsSets([1,3,2,2]);
您可以使用最新版本中提供的
成员
测试:

expect([4, 2]).to.have.members([2, 4]);
expect([5, 2]).to.not.have.members([5, 2, 1]);


请注意,该方法的名称并不是很好,它太晚了,我也没有想出更好的方法;)请注意,这不是无序的相等测试,而是设置相等。两个数组中都允许重复项;例如:
expect([1,2,3])to.be.equalAsSets([1,3,2,2])
[1, 2, 3].should =~ [1, 2]
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([4, 2]).to.have.members([2, 4]);
expect([5, 2]).to.not.have.members([5, 2, 1]);
expect([4, 2]).to match_array([2, 4])