2016-07-11 93 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); 
相關問題