2016-07-11 92 views

回答

2

幼稚,但可能足以解决方案是比较之前的数组进行排序:

should(a.sort()).be.eql(b.sort()) 

注意sort() works in-place,变异原阵列。

+1

这个答案不正确。 .equal使用===参考相等。需要使用.eql。 –

+0

@denbardadym感谢您的注意,更新了我的答案。 – Timo

1

您可以通过shouldAssertion.add功能实现此功能。例如:

var a = ['a', 'as', 'sa']; 
var b = ['sa', 'a', 'as']; 

should.Assertion.add('haveSameItems', function(other) { 
    this.params = { operator: 'to be have same items' }; 

    this.obj.forEach(item => { 
    //both arrays should at least contain the same items 
    other.should.containEql(item); 
    }); 
    // both arrays need to have the same number of items 
    this.obj.length.should.be.equal(other.length); 
}); 

//passes 
a.should.haveSameItems(b); 

b.push('d'); 

// now it fails 
a.should.haveSameItems(b); 
相关问题