javascript - 如果结果返回,如何在 postman 中验证而不考虑结果数量

标签 javascript node.js postman postman-collection-runner

如何在 postman 中验证响应中的所有数据返回 id、名字、姓氏等的结果数量,而不管结果数量

这是响应的样子:

[
    {
        "id": 1,
        "first_name": "Sebastian",
        "last_name": "Eschweiler",
        "email": "sebastian@codingthesmartway.com"
    },
    {
        "first_name": "Sebastian",
        "last_name": "Eschweiler",
        "email": "sebastian@codingthesmartway.com",
        "id": 4
    },
    {
        "id": 5,
        "first_name": "Sebastian",
        "last_name": "Eschweiler",
        "email": "sebastian@codingthesmartway.com"
    },
    {
        "first_name": "Sebastian",
        "last_name": "Eschweiler",
        "email": "sebastian@codingthesmartway.com",
        "id": 8
    },
    {
        "id": 9,
        "first_name": "Sebastian",
        "last_name": "Eschweiler",
        "email": "sebastian@codingthesmartway.com"
    },
    {
        "first_name": "Sebastian",
        "last_name": "Eschweiler",
        "email": "sebastian@codingthesmartway.com",
        "id": 12
    }
]

我想验证两件事:

1) 响应返回id, first_name, last_name, email

2) 无论只有一个结果还是 100 个结果,所有 first_name 都等于“Sebastian”

这是我尝试过的方法,但它只适用于一个结果:

const jsonData = pm.response.json();

pm.test('Has data', function() {
  pm.expect(jsonData).to.have.property('first_name');
  pm.expect(jsonData).to.have.property('last_name');
  pm.expect(jsonData).to.have.property('email');
  pm.expect(jsonData).to.have.property('id');

});

最佳答案

你可以试试这个:

pm.test("Has data",() => {
    _.each(pm.response.json(), (item) => {
      pm.expect(item.first_name).to.eql("Sebastian")
      pm.expect(item).to.have.property('first_name')
      pm.expect(item).to.have.property('last_name')
      pm.expect(item).to.have.property('email')
      pm.expect(item).to.have.property('id')
    })
})

这将根据您提供的数据集工作。

关于javascript - 如果结果返回,如何在 postman 中验证而不考虑结果数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53000567/

相关文章:

javascript - 从 Angular Controller 中的 php 文件获取 JSONP 数据

javascript - 如何使用正则表达式从字符串中提取所有 24 小时时间?

javascript - 如何获取 Mongoose 模型中已填充 child 的数量?

spring-boot - 如何确定向远程 Spring Boot 服务器发送请求的 URL?

Postman - 将 ISO 格式日期转换为 UNIX 时间戳

java - 带有 json Body 和多部分文件数组的 PUT 请求

javascript - 有没有像 Joi 这样的 JS 智能 json 模式验证器,但是有动态自定义错误?

javascript - 如何限制在文本区域中输入的字符数

node.js - 永远使用 --nouse-idle-notification 和其他标志启动 Node.js

node.js - 如何在 Telegraf 中处理 contactRequest