gpt4 book ai didi

javascript - 如何知道 postman 中是否存在对象,如下所示:

转载 作者:行者123 更新时间:2023-12-01 01:34:17 26 4
gpt4 key购买 nike

如何知道 postman 中是否存在对象,如下所示,我想检查所有参数。如果全部正确返回

[
{
"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、logo、createdAt、UpdatedAt、标签等都存在,并且希望创建一个全局函数,因此在其他测试中我只需调用一次即可

最佳答案

要修复您的错误,请尝试循环遍历您的对象数组,即jsonData,如下所示。顺便说一句,我没有玩 postman 全局函数创建。希望此链接能够帮助您了解如何将其制作/使用为全局 How to Write Global Functions in Postman

pm.test('Has data', () => {
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');
})
});

关于javascript - 如何知道 postman 中是否存在对象,如下所示:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52979745/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com