在 POSTMAN 断言中验证 return json 响应不超过一个

Verify the return json reponse is not more than one in POSTMAN assert

我在 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

单个 Json 对象没有长度 属性,因此您不能那样做。一个建议是将 Json 对象推入数组,然后您可以验证长度:

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

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

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