2016-07-11 14 views

Antwort

2

Eine naive, aber möglicherweise eine ausreichende Lösung wäre, die Arrays zu sortieren, bevor sie zu vergleichen:

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

Beachten Sie, dass sort() works in-place, die ursprünglichen Arrays mutiert.

+1

Diese Antwort ist nicht korrekt. .equal verwendet === für die Referenzgleichheit. Brauchen Sie .eql. –

+0

@denbardadym Danke für den Hinweis, aktualisiert meine Antwort. – Timo

1

Sie können dies mit Assertion.add Feature implementieren. Zum Beispiel:

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);