json - Postman 测试 - 对象的架构

标签 json testing postman

我有一个响应主体:

{
    "Id": 15,
    "Name": "Carrier1",
    "Fein": "Fein1",
    "McNumber": "McNumber1",
    "DotNumber": "DotNumber1",
    "Address": {
        "Street": "Street1",
        "City": "City1",
        "ZipPostalCode": null,
        "StateName": "AA (Armed Forces Americas)",
        "StateAbbr": "AA",
        "ContactName": null,
        "ContactPhone": null,
        "ContactFaxNumber": null,
        "ContactEmail": null
    }
}

我使用 Postman 并想在测试中描述用于验证的模式:

const schema = {
  "required": ["Id"],
  "properties": {
    "Id": {
      "type": "integer",
    },
    "Name": {
      "type": "string",
    },
    "Fein": {
      "type": "string",
    },
    "McNumber": {
      "type": "string",
    },
    "DotNumber": {
      "type": "string",
    },
    "Address": {
        "type" : {
            "properties": {
                "Street": {
                "type": "string",
                },            
            },
        }
    }
  }
};

var carrier = JSON.parse(responseBody);
tests["Carrier is valid"] = tv4.validate(carrier, schema);

但它不起作用。验证它应该是对象:

"Address": {
    "type" : "object"
    }

工作正常。如何描述它的细节?

最佳答案

这行得通吗:

const schema = {
  "required": ["Id"],
  "properties": {
      "Id": {
          "type": "integer"
      },
      "Name": {
          "type": "string"
      },
      "Fein": {
          "type": "string"
      },
      "McNumber": {
          "type": "string"
      },
      "DotNumber": {
          "type": "string"
      },
      "Address": {
          "type" : "object",
          "properties": {
              "Street": {
                  "type": "string"
              }
          }
        }
    }
}

添加此测试以检查:

pm.test('Schema Valid', () => {
    var carrier = pm.response.json()
    pm.expect(tv4.validate(carrier, schema)).to.be.true
})

Test Passing

我正在使用 native Postman 应用程序,因此如果您仍在使用 Chrome 扩展程序,这将失败,因为它不知道 pm.* API 函数

关于json - Postman 测试 - 对象的架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52335285/

相关文章:

javascript - Protractor - 切换到窗口或 iframe 后等待

spring - Cucumber 测试用例一起运行时会失败,但当我单独运行它们时它们会通过

Postman 测试脚本 : what version of JavaScript

json - REST API 返回 "bad array"而不是 JSON 对象

postman - postman 收藏保存在哪里?

json - 我有复杂的 JSON 数据,我需要创建一个 HTML 表单来轻松编辑它。最快的方法?

JavaScript 和 Node.js - Controller 函数中的 JSON 字符串不正确

javascript - 如何删除两个具有相同id的json对象

jquery - 为什么我的 jQuery jEditable、就地编辑回调不起作用?

testing - "Mock"带有 Protractor、Cucumber 和 Chai 的后端 http 响应