使用 Chai 匹配 JSON 对象数组中的数组元素
Match elements of an array from a JSON Array of Objects using Chai
假设我有一个数组
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
进行断言。使用 include
和 deep.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 )
})
})
嗯,这就是我会做的。这很简单 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
else false
.
我将名称列表中的 "Jack" 更改为 "Jake" 并从响应数据中删除 "Adam" 以确保每个名称都出现在数据中以证明下面函数的使用。
函数前面的 JS 文档描述了它的作用。
/*
* 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; }
您可以直接对 Chakram 响应使用 Chai 断言。
const namesArray = [];
resp.map(person => namesArray.push(person.person_name));
names.every(name => expect(namesArray).to.include(name));
});
假设我有一个数组
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
进行断言。使用 include
和 deep.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 )
})
})
嗯,这就是我会做的。这很简单 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
else false
.
我将名称列表中的 "Jack" 更改为 "Jake" 并从响应数据中删除 "Adam" 以确保每个名称都出现在数据中以证明下面函数的使用。
函数前面的 JS 文档描述了它的作用。
/*
* 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; }
您可以直接对 Chakram 响应使用 Chai 断言。
const namesArray = [];
resp.map(person => namesArray.push(person.person_name));
names.every(name => expect(namesArray).to.include(name));
});