【问题标题】:Verify the return json reponse is not more than one in POSTMAN assert验证 POSTMAN assert 中返回的 json 响应不超过一个
【发布时间】:2019-03-31 08:05:49
【问题描述】:

我在 POSTMAN 中有返回的响应:

{
    "first_name": "Abc",
    "last_name": "dbc",
    "email": "abc@example.com",
    "id": 46
}

我如何验证返回的结果json长度不超过1,这是我尝试过的:

const jsonData = pm.response.json();

pm.test('Has data and only returns 1 result', 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.length).to.equal(1);

});

但是,我收到此错误:

Has data and only returns 1 result | AssertionError: expected undefined to equal 1

【问题讨论】:

  • 对象没有长度属性,只有数组。你到底想检查什么?如果前 3 个检查通过,它看起来像返回一个对象而不是一个数组。
  • 是的,其他通行证。那我应该测试什么?
  • 不太清楚你在问什么或试图测试什么
  • 我正在尝试验证它只返回一个如上所述的 json。我已经更新了我的问题

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


【解决方案1】:

单个 Json 对象没有长度属性,所以你不能那样做。一个建议是让您将 Json 对象推送到一个数组中,然后您可以验证长度:

var json = pm.response.json();
var data = [];
data.push(json);

tests["Test length of array"] = data === 4;

我实际上并没有测试这段代码,但我认为它应该可以工作,或者你可以尝试一下这个想法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-29
    • 2022-01-17
    • 2018-03-11
    • 2020-12-16
    • 2021-02-10
    • 2017-12-11
    • 1970-01-01
    • 2022-10-18
    相关资源
    最近更新 更多