2016-07-11 5 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);