比较 Postman 中的整个响应主体,同时它具有 JSON DataSchema
Comparing entire response body in Postman, while it has JSON DataSchema
我对 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。我试着放更多 \
但 Postman 会抱怨。
您的测试中有很多空格。尝试:
pm.response.to.have.body("{\"Id\":"+typeId+",\"NamespaceId\":"+namespaceId+",\"Name\":\"AutoTest\" ,\"DataSchema\":\"{\n"firstName\": {\n"type\": \"string\",\n"description\": \"The person\u0027s firstname.\"\ n}\n}\",\"Code\":\"AUTOTYPE\"}");
我认为问题在于您指定希望将字符串作为正文,而不是将其与 JSON 对象进行比较。您现在设置的方式,测试非常脆弱,恕我直言。
多一点编码会给你一个更稳定的解决方案(例如,不依赖于空白格式,这与 JSON 无关):
pm.test("Body is correct", function () {
const jsonData = pm.response.json();
pm.expect(jsonData.Id)
.to.be.a('number', 'Id should be a number')
.that.is.equal(typeId, 'Id should equal typeId');
pm.expect(jsonData.NamespaceId)
.to.be.a('number', 'NamespaceId should be a number')
.that.is.eql(namespaceId);
pm.expect(jsonData.Name).to.equal("Auto Test");
// is this really what you want, i.e. should this be a string?
pm.expect(jsonData.DataSchema).to.be.a('string');
const dataSchema = JSON.parse(jsonData.DataSchema);
pm.expect(dataSchema.firstName.type).to.equal("string");
// ... etc
});
我对 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。我试着放更多 \
但 Postman 会抱怨。
您的测试中有很多空格。尝试:
pm.response.to.have.body("{\"Id\":"+typeId+",\"NamespaceId\":"+namespaceId+",\"Name\":\"AutoTest\" ,\"DataSchema\":\"{\n"firstName\": {\n"type\": \"string\",\n"description\": \"The person\u0027s firstname.\"\ n}\n}\",\"Code\":\"AUTOTYPE\"}");
我认为问题在于您指定希望将字符串作为正文,而不是将其与 JSON 对象进行比较。您现在设置的方式,测试非常脆弱,恕我直言。
多一点编码会给你一个更稳定的解决方案(例如,不依赖于空白格式,这与 JSON 无关):
pm.test("Body is correct", function () {
const jsonData = pm.response.json();
pm.expect(jsonData.Id)
.to.be.a('number', 'Id should be a number')
.that.is.equal(typeId, 'Id should equal typeId');
pm.expect(jsonData.NamespaceId)
.to.be.a('number', 'NamespaceId should be a number')
.that.is.eql(namespaceId);
pm.expect(jsonData.Name).to.equal("Auto Test");
// is this really what you want, i.e. should this be a string?
pm.expect(jsonData.DataSchema).to.be.a('string');
const dataSchema = JSON.parse(jsonData.DataSchema);
pm.expect(dataSchema.firstName.type).to.equal("string");
// ... etc
});