【问题标题】:Postman test error while trying to check if JSON field value is not null or undefined尝试检查 JSON 字段值是否不为空或未定义时邮递员测试错误
【发布时间】:2021-03-06 11:05:48
【问题描述】:

我正在尝试在 Postman 中为 GET 请求创建一个测试,以检查存储在每个对象的“名称”字段中的值是否不同于 nullundefined,但我收到以下消息“TypeError:无法读取未定义的属性‘名称’” 这是我正在使用的 JSON 正文的示例:

[
{
 "id":1,
 "name": "Peter"
},
{
 "id":2,
 "name":"Stewie" 
},
{
 "id":3,
 "name":"Brian"
}
]

这是测试的JS代码:

pm.test(``, function(){ 
    var response = JSON.parse(responseBody)
    var result = true

    for (i=0;i<=response.length;i++){ 
    if(!response[i].name){
     result = false}
    }
    pm.expect(result).to.eql(true)
});

我尝试过的事情:

  1. 使用获取 JSON 正文 var jsonData = pm.response.json(); 代替 var response = JSON.parse(responseBody)
  2. IF 语句使用以下条件: !response[i]['name']
  3. 将“名称”值存储在全局变量中,如下所示:
    pm.test(``, function(){ 
        var response = JSON.parse(responseBody)
        var result = true
    
        for (i=0;i<=response.length;i++){ 
         pm.globals.set("variable_name", response[i]['name'];
         var name = pm.globals.get("variable_name");
        if(!name){
         result = false}
        }
        pm.expect(result).to.eql(true)
       });

谢谢!

【问题讨论】:

  • 你为什么不只是检查它是什么类型,如果不是那种类型,它会失败? pm.expect(response[i].name).to.be.a('string');

标签: json testing postman


【解决方案1】:

它是一个超出索引错误的数组,你正在检查 i 直到它等于 response.lenght

所以最后一个值将是 response[response.length] ,没有元素最后一个元素是 response[response.length-1] 。所以只需从 for 循环中删除 equal

pm.test(``, function(){ 
    var response = JSON.parse(responseBody)
    var result = true

    for (i=0;i<response.length;i++){ 
    if(!response[i].name){
     result = false}
    }
    pm.expect(result).to.eql(true)
});

但正确的做法是进行模式验证:

let schema = {
    "$schema": "http://json-schema.org/draft-07/schema",
    "$id": "http://example.com/example.json",
    "type": "array",
    "title": "The root schema",
    "description": "The root schema comprises the entire JSON document.",
    "default": [],
    "examples": [
        [
            {
                "id": 1,
                "name": "Peter"
            },
            {
                "id": 2,
                "name": "Stewie"
            }
        ]
    ],
    "additionalItems": true,
    "items": {
        "$id": "#/items",
        "anyOf": [
            {
                "$id": "#/items/anyOf/0",
                "type": "object",
                "title": "The first anyOf schema",
                "description": "An explanation about the purpose of this instance.",
                "default": {},
                "examples": [
                    {
                        "id": 1,
                        "name": "Peter"
                    }
                ],
                "required": [
                    "id",
                    "name"
                ],
                "properties": {
                    "id": {
                        "$id": "#/items/anyOf/0/properties/id",
                        "type": "integer",
                        "title": "The id schema",
                        "description": "An explanation about the purpose of this instance.",
                        "default": 0,
                        "examples": [
                            1
                        ]
                    },
                    "name": {
                        "$id": "#/items/anyOf/0/properties/name",
                        "type": "string",
                        "title": "The name schema",
                        "description": "An explanation about the purpose of this instance.",
                        "default": "",
                        "examples": [
                            "Peter"
                        ]
                    }
                },
                "additionalProperties": true
            }
        ]
    }
}


pm.test("Status code is 200", function () {
     pm.response.to.have.jsonSchema(schema);
});

您可以使用以下方式生成架构:https://jsonschema.net/home

名称为空时的邮递员输出:

【讨论】:

  • 感觉不像是解决问题的方法,而且是一种非常混乱的寻找答案的方法。在我看来。
  • 是的,丹尼只是过度设计了这些东西
  • 最好的方法是使用字符串类型进行模式验证,如果数据为空会抛出错误
  • @DannyDainton 补充道
  • @PDHide 显然 facepalm ...我完全错过了这一点。谢谢!
猜你喜欢
  • 1970-01-01
  • 2019-10-06
  • 2016-07-20
  • 2022-09-23
  • 2021-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多