如何使用超级测试检查数组是否包含定义的值?

How to check if array contains defined values using supertest?

假设在测试期间作为回应,我有这样的数组:

array = [
 'a',   'b',
 'c',   'd',
 'e',   'f',
 'g',   'h',
 'i',   'j'
]

我想检查这个数组是否真的包含这些值:

array.should.be.a.Array()
.with.lengthOf(response.body.length)
.and.have.properties('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j');

如您所见,我不能在这里使用 have.properties 因为它是数组,而不是对象。那么,我该如何检查?

如果需要精确匹配,请使用 Chandan 的答案,如果需要匹配子集,则可以使用 should.containDeep(请参阅 docs)。这是一个例子:

const arr = [
 'a',   'b',
 'c',   'd',
 'e',   'f',
 'g',   'h',
 'i',   'j'
];

arr.should.containDeep(['a', 'b', 'c', 'd']);
console.log("test passed");
<script src="https://cdnjs.cloudflare.com/ajax/libs/should.js/13.2.3/should.min.js"></script>

您可以使用 should.deepEqual 将长度和值与顺序匹配

const arr = [
 'a',   'b',
 'c',   'd',
 'e',   'f',
 'g',   'h',
 'i',   'j'
];

arr.should.deepEqual(arr);
console.log("test passed");
<script src="https://cdnjs.cloudflare.com/ajax/libs/should.js/13.2.3/should.min.js"></script>