"Postman -How to check the typeof any key is 'string' or 'null' at same time"

"Postman -How to check the typeof any key is 'string' or 'null' at same time"

在给定的响应片段 "parentName" 中,类型有时是 null 或有时是 string。如何 check/write 测试用例一次检查 stringnull 的类型。

tests["Verify parentName is string"] = typeof(jsonData.parentName) === "string" || null;

tests["Verify parentName is string"] = typeof(jsonData.parentName) === "string" || "null";
"demo": [
            {
                "id": 68214,
                "specializationId": 286,
                "name": "Radiology",
                "parentName": null,
                "primary": true
            }
        ],

如何在 postman 中处理这种情况(null & string)。

我不会在 Postman 测试用例中推荐 if elseif。 Postman 具有用于模式检查的内置功能,您可以使用它并且可以在没有 if else 的情况下获得相同的结果。

首先,我正在考虑将以下内容作为回应:

{
    "demo": [{
        "id": 68214,
        "specializationId": 286,
        "name": "Radiology",
        "parentName": null,
        "primary": true
    }]
}

邮递员测试应该是:

var Ajv = require('ajv'),
ajv = new Ajv({logger: console}),
schema = {
    "properties": {
        "parentName": {
            "type":["string", "null"]
        }
    }
};

pm.test('Verify parentName is string', function() {    
    var resParentName =  pm.response.json().demo[0].parentName;
    pm.expect(ajv.validate(schema, {parentName: resParentName})).to.be.true;
});

编辑: 验证完整的响应,而不仅仅是第一项。还要检查 parentName 是否存在于响应中。

var Ajv = require('ajv'),
ajv = new Ajv({logger: console}),
schema = {
    "properties": {
        "demo":{
            "type": "array",
            "items": {
                 "properties": {
                     "id":{ "type": "integer" },
                     "specializationId":{ "type": "integer" },
                     "name":{"type": "string"},
                     "parentName":{
                         "type":["string", "null"]
                     },
                     "primary":{"type": "boolean"}
                 },
                 "required": [ "parentName"]
            }
        }
    }
};

pm.test('Validate response', function() {
    pm.expect(ajv.validate(schema, pm.response.json())).to.be.true;
});