赛普拉斯API测试.找不到属性



我正在为我的API开发Cypress测试。我的API在Postman上的回复如下:

{"infected" : false}

我的Cypress测试如下:

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);
});

最新更新