我正在为我的 API 开发 Cypress 测试。 我在 Postman 中的 API 的响应如下:
{"infected" : false}
我的赛普拉斯测试如下:
describe("Testing the result after scanning file", () => {
it("Scan file", function () {
//Declarations
const fileName = 'example.json';
cy.fixture(fileName, 'binary')
.then((file) => Cypress.Blob.binaryStringToBlob(file))
.then((blob) => {
const formData = new FormData();
formData.append("file", blob, fileName);
cy.request({
method: 'POST',
headers: {
'content-type': 'multipart/form-data'
},
body: formData,
url: '/scan'
}).then(response => {
console.log('the response is: ', response.body)
expect(response.body).to.have.property('infected').and.eq(false);
});
})
});
});
在我的浏览器中,Cypress 测试失败并显示消息:
assert expected {} to have property infected
我真的已经被这个问题伤透了脑筋,仍然不知道如何解决它。谁能告诉我出了什么问题?
尝试将响应转换为 json,您可能会看到数据的字符串版本。
Postman 输出不会有帮助,它可能会在后台自动转换。
cy.request({
...
})
.then(response => response.json())
// OR
// .then(response => response.body.json())
.then(data => {
console.log('the data is: ', data) // better debug tool than Postman
expect(data).to.have.property('infected').and.eq(false);
});
@Simon-我终于弄清楚出了什么问题。请求正文存储为 ArrayBuffer。我将在下面添加我的答案。
这个问题的答案是什么。我也面临同样的问题。文件上传后响应正文为空白