在Postman中具有JSON DataSchema的情况下比较整个响应正文

时间:2019-02-08 00:04:02

标签: json postman web-api-testing

我对Postman还是很陌生,并尝试创建自动化脚本来创建一个包含JSON模式的对象。但是我遇到一个错误,不确定如何绕过。

可以帮忙吗?

这是我期望的响应脚本:

    pm.test("Body is correct", function () {
    pm.response.to.have.body("{\"Id\":"+typeId+",\"NamespaceId\":"+namespaceId+",\"Name\":\"Auto Test\",\"DataSchema\":\"\{\n    \"firstName\": {\n           \"type\": \"string\",\n           \"description\": \"The person\u0027s first name.\"\n    \}\n}\",\"Code\":\"AUTOTYPE\"}");
});

这是实际答复(正文):

    {
    "Id": 1059,
    "NamespaceId": 1089,
    "Name": "Auto Test",
    "DataSchema": "{\r\n  \"firstName\": {\r\n    \"type\": \"string\",\r\n    \"description\": \"The person's first name.\"\r\n  }\r\n}",
    "Code": "AUTOTYPE"
}

这是我遇到的错误:

Body is correct | AssertionError: expected response body to equal '{"Id":1059,"NamespaceId":1089,"Name":"Auto Test","DataSchema":"{\n "firstName": {\n "type": "string",\n "description": "The person\'s first name."\n }\n}","Code":"AUTOTYPE"}' but got '{"Id":1059,"NamespaceId":1089,"Name":"Auto Test","DataSchema":"{\\r\\n \\"firstName\\": {\\r\\n \\"type\\": \\"string\\",\\r\\n \\"description\\": \\"The person\\u0027s first name.\\"\\r\\n }\\r\\n}","Code":"AUTOTYPE"}'

这是创建脚本的实际部分:

    {  "NamespaceId": 1089,
  "Name": "Auto Test",
  "Code": "AUTOTYPE",
  "DataSchema": {
    "firstName": {
           "type": "string",
           "description": "The person's first name."
    }
}
 }

谢谢。

P.S。我尝试放更多\,但后来邮递员会抱怨。

2 个答案:

答案 0 :(得分:1)

我认为问题在于您指定希望将字符串作为正文,而不是将其与JSON对象进行比较。您现在的设置方式,该测试非常脆弱,远远超出了需要的恕我直言。

多一点编码将为您提供更稳定的解决方案(例如,不依赖于空格格式,这与JSON无关):

'add', 'change', 'delete', 'view'

答案 1 :(得分:0)

您的测试中有很多空格。试试:

pm.response.to.have.body(“ {\” Id \“:” + typeId +“,\” NamespaceId \“:” + namespaceId +“,\” Name \“:\” AutoTest \“,\ “ DataSchema \”:\“ {\ n” firstName \“:{\ n” type \“:\”字符串\“,\ n”描述\“:\”此人的名字。\“ \ n} \ n} \“,\”代码\“:\” AUTOTYPE \“}”);