4
我有两个数组:Should.js:检查两个数组包含相同字符串
var a = ['a', 'as', 'sa'];
var b = ['sa', 'a', 'as'];
有什么特别的shouldJS测试,如果这两个数组有相同的项目?任何类似
should(a).be.xyz(b)
可以测试它们吗?在这里,xyz是我正在寻找的。
我有两个数组:Should.js:检查两个数组包含相同字符串
var a = ['a', 'as', 'sa'];
var b = ['sa', 'a', 'as'];
有什么特别的shouldJS测试,如果这两个数组有相同的项目?任何类似
should(a).be.xyz(b)
可以测试它们吗?在这里,xyz是我正在寻找的。
您可以通过should
的Assertion.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);
这个答案不正确。 .equal使用===参考相等。需要使用.eql。 –
@denbardadym感谢您的注意,更新了我的答案。 – Timo