如何知道邮递员中是否存在对象,如下所示,我想检查所有参数。如果所有的都正确返回了
[
{
"id": "MnnRVEifcngi2",
"givenName": "Witting and Sons",
"logo": "http://lorempixel.com/640/480/business",
"createdAt": "Fri Jan 05 2018 07:54:09 GMT+0000 (UTC)",
"updatedAt": "Tue Oct 23 2018 22:25:54 GMT+0000 (UTC)",
"tags": [
"Web",
"Paradigm"
]
},
{
"id": "E7z9UujhROF2L",
"givenName": "Block Group",
"logo": "http://lorempixel.com/640/480/business",
"createdAt": "Mon Feb 05 2018 11:50:00 GMT+0000 (UTC)",
"updatedAt": "Wed Oct 24 2018 13:29:35 GMT+0000 (UTC)",
"tags": [
"Brand",
"Web"
]
},
{
"id": "MzbqnzFImpbkf",
"givenName": "Dickinson - Ziemann",
"logo": "http://lorempixel.com/640/480/business",
"createdAt": "Fri Feb 02 2018 07:00:32 GMT+0000 (UTC)",
"updatedAt": "Tue Oct 23 2018 18:11:30 GMT+0000 (UTC)",
"tags": [
"Applications"
]
},
{
"id": "3-vqC_QG5Up8r",
"givenName": "Lindgren - Mitchell",
"logo": "http://lorempixel.com/640/480/business",
"createdAt": "Sun May 06 2018 07:24:02 GMT+0000 (UTC)",
"updatedAt": "Wed Oct 24 2018 10:10:23 GMT+0000 (UTC)",
"tags": [
"Branding",
"Mobility",
"Functionality"
]
},
{
"id": "8dUhM_0j5vloD",
"givenName": "Schmitt LLC",
"logo": "http://lorempixel.com/640/480/business",
"createdAt": "Wed Sep 12 2018 01:16:49 GMT+0000 (UTC)",
"updatedAt": "Wed Oct 24 2018 03:40:15 GMT+0000 (UTC)",
"tags": [
"Accounts",
"Data"
]
},
{
"id": "zl_43QRBDWBnW",
"givenName": "Barton - Bauch",
"logo": "http://lorempixel.com/640/480/business",
"createdAt": "Tue Dec 05 2017 14:12:00 GMT+0000 (UTC)",
"updatedAt": "Tue Oct 23 2018 19:36:52 GMT+0000 (UTC)",
"tags": [
"Response",
"Accountability",
"Identity"
]
},
{
"id": "kxgqxbXBS53_2",
"givenName": "Lind Inc",
"logo": "http://lorempixel.com/640/480/business",
"createdAt": "Tue May 22 2018 16:37:33 GMT+0000 (UTC)",
"updatedAt": "Wed Oct 24 2018 06:35:53 GMT+0000 (UTC)",
"tags": [
"Communications",
"Brand"
]
},
{
"id": "xWAIuoDY5icIl",
"givenName": "Gutkowski - Hickle",
"logo": "http://lorempixel.com/640/480/business",
"createdAt": "Tue Jun 19 2018 21:29:59 GMT+0000 (UTC)",
"updatedAt": "Wed Oct 24 2018 14:02:39 GMT+0000 (UTC)",
"tags": [
"Metrics",
"Infrastructure",
"Accounts"
]
}
]
这是我正在尝试但出现错误的内容:
const jsonData = pm.response.json();
pm.test('Has data', function() {
pm.expect(jsonData).to.have.property('id');
});
我要验证-id,givenName,徽标,createdAt,UpdatedAt,标记等全部存在,并希望创建全局函数,因此在其他测试中我只能调用一次
答案 0 :(得分:2)
要解决您的错误,请尝试遍历对象数组,即如下所示的jsonData
。顺便说一下,我没有玩邮递员全局函数创建。希望此链接对您如何将其用作全局How to Write Global Functions in Postman
pm.test('Has data', function() {
jsonData.forEach(row=>{
pm.expect(row).to.have.property('id');
pm.expect(row).to.have.property('givenName');
pm.expect(row).to.have.property('logo');
pm.expect(row).to.have.property('createdAt');
pm.expect(row).to.have.property('updatedAt');
pm.expect(row).to.have.property('tags');
})
});
答案 1 :(得分:0)
要检查所有参数作为响应,您应该考虑使用内置的tv4.validate
,它代表 Tiny Validator(用于v4 JSON模式)-它根据提供的值验证简单值和复杂对象模式。
在您的特定情况下,您将执行以下操作:
const jsonData = pm.response.json();
const schema = {
items: {
type: "object",
properties: {
id: {type: "string"},
givenName: {type: "string"},
logo: {type: "string"},
createdAt: {type: "string"},
updatedAt: {type: "string"},
tags: {type: "array"}
},
required:["id","givenName","logo", "createdAt", "updatedAt", "tags"]
}
};
pm.test('Schema is valid', () => pm.expect(tv4.validate(jsonData, schema)).to.be.true);
有关如何使用tv4的更多示例,请参见其repo。