使用Chai匹配来自JSON对象数组的数组元素

时间:2018-04-17 12:29:57

标签: javascript chai assertions chakram

假设我有一个数组

let array_of_string = ['John','Jack','Smith','Ryan'];

如何断言这些键包含在JSON数组对象中,其中JSON就是这样的

[ {
    "person_name": "Jake",
    "person_id": '1234',
    "employee_type": "Regular"
}, {
    "person_name": "Adam",
    "person_id": '1245',
    "employee_type": "Contractor"
}, {
    "person_name": "John",
    "person_id": '2342',
    "employee_type": "Regular"
}, {
    "person_name": "Smith",
    "person_id": '3456',
    "employee_type": "Contractor"
}, {
    "person_name": "Ryan",
    "person_id": '0123',
    "employee_type": "Regular"
} ]

我使用Chai来获取断言。使用includedeep.Equal似乎不起作用。

JSON数组是API的响应。所以目前我试过这个

it('checks if the elements are in API response', () => {
  return response.then(function(resp){
    expect(resp).to.be.an('array').that.includes(array_of_string )
  })
})

3 个答案:

答案 0 :(得分:1)

嗯,这就是我要做的。这是简单的JS



'use strict';

var array_of_string = ['John', 'Jack', 'Smith', 'Ryan', 'Usman'];
var list = [{
    "person_name": "Jake",
    "person_id": '1234',
    "employee_type": "Regular"
}, {
    "person_name": "Adam",
    "person_id": '1245',
    "employee_type": "Contractor"
}, {
    "person_name": "John",
    "person_id": '2342',
    "employee_type": "Regular"
}, {
    "person_name": "Smith",
    "person_id": '3456',
    "employee_type": "Contractor"
}, {
    "person_name": "Ryan",
    "person_id": '0123',
    "employee_type": "Regular"
}];
var b = true;
array_of_string.forEach(function (name) {

    if (list.some(e=>e.person_name === name)) {} else {
        b = false;
        return false;
    }
});

console.log(b);




如果array_of_string的所有元素都作为person_name属性存在,则应记录true其他false

答案 1 :(得分:1)

我改变了#34;杰克"杰克"杰克"在名单中删除" Adam"从响应数据中确保数据中出现的每个名称都证明了以下功能的使用。

该函数之前的JS Doc描述了它的作用。



/*
 * Check if each value exist (at least once) in a list (by its key).
 * @param {object[]} list - List of objects.
 * @param {string[]} vals - List of values to search for.
 * @param {string}   key  - Key for each object to check the value.
 * @return {boolean} Returns wheteher all values exist in the list.
 */
const fn = (list, vals, key) => vals.every(val => list.some(e => e[key] === val));

var names = [ 'Jake', 'John', 'Smith', 'Ryan' ];
var responseData = [{
  "person_name": "Jake",
  "person_id": '1234',
  "employee_type": "Regular"
}, {
  "person_name": "John",
  "person_id": '2342',
  "employee_type": "Regular"
}, {
  "person_name": "Smith",
  "person_id": '3456',
  "employee_type": "Contractor"
}, {
  "person_name": "Ryan",
  "person_id": '0123',
  "employee_type": "Regular"
}];

// names.every(name => responseData.some(p => p['person_name'] === name));
console.log('All names present?', fn(responseData, names, 'person_name'));

.as-console-wrapper { top: 0; max-height: 100% !important; }




答案 2 :(得分:0)

你可以直接在Chakram回应中使用Chai断言。

 const namesArray = [];
 resp.map(person => namesArray.push(person.person_name));
 names.every(name => expect(namesArray).to.include(name));
});