当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Chai Array to contain any member?

问题描述:

I'm currently querying a database documents that contain the category: foo or bar. However I am finding it quite difficult to test this.

Here is a sample of the array that I want to test:

 var array = [

{category: ['foo'] },

{category: ['foo', 'bar'] },

{category: ['foo'] }

];

expect(array[0].category).to.include.any.members(['foo', 'bar']);

However it doesn't seem to work properly, it looks for each member to be present.

I would like the test to be able to check each element of the array to include foo OR bar.

网友答案:

It seems there is no or method or something like this in Chai.

So I think you must loop through your array like this:

var array = [
    {category: ['foo'] }, 
    {category: ['foo', 'bar'] }, 
    {category: ['foo'] }
];

array.forEach(function(item) {
    expect(item).to.have.any.keys('category');

    item.category.forEach(function(catItem) {
        expect(catItem).to.match(/foo|bar/);
    });
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: