Should.js:检查两个数组是否包含相同的string
我有两个数组:
var a = ['a', 'as', 'sa']; var b = ['sa', 'a', 'as'];
应该有什么特别的东西来testing这两个数组是否有相同的项目? 任何喜欢
should(a).be.xyz(b)
那可以testing他们? 在这里,xyz是我正在寻找的。
一个天真的,但也许是足够的解决scheme将是比较之前sorting数组:
should(a.sort()).be.eql(b.sort())
请注意, sort()
在原地工作 ,突变原始数组。
你可以用should
的Assertion.add
特性来实现这个function。 例如:
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);