我在邮递员中有返回的响应:
{
"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;
我实际上没有测试这段代码,但我认为它应该可以工作,或者你可以玩这个想法。