對于測驗,我需要能夠將下載的 json 檔案中的值與來自 graphql 查詢的值進行比較。這將使我能夠驗證下載檔案上的值是否正確。
到目前為止,我已經能夠從 json 檔案中檢索資料并進行 db 呼叫,并且在控制臺記錄時兩者都是可見且匹配的,如下所示:


但是,當我比較它們并在我的斷言中將值更改為不正確時,斷言似乎仍在傳遞。檢查的代碼如下:
const downloadsFolder = Cypress.config('downloadsFolder');
cy.readFile(path.join(downloadsFolder, 'test.json'), { timeout: 30000 })
.should('exist')
.then((data) => {
cy.graphQlQuery(url, getUserReport())
.then((response: any) => {
console.log(response);
console.log(data);
expect(response.status).to.eq(200);
expect(data.length === response.body.data.workforce_roles.length);
expect(JSON.stringify(data[0]) === JSON.stringify(response.body.data.workforce_roles));
debugger;
});
});
誰能告訴我這段代碼有什么問題以及如何修復它,以便當這些值不匹配時我正確收到錯誤。我懷疑這與 cypress 在繼續之前沒有等待檢查完成這一事實有關,但我不確定如何糾正給定 cypress 與 async/await 的麻煩關系。
uj5u.com熱心網友回復:
您需要限定最后兩個期望。賽普拉斯無法識別它們
expect(data.length === response.body.data.workforce_roles.length).to.eq(true)
//or
expect(data.length).to.eq(response.body.data.workforce_roles.length)
expect(JSON.stringify(data[0]) === JSON.stringify(response.body.data.workforce_roles)).to.eq(true)
//or
expect(data[0]).to.deep.eq(response.body.data.workforce_roles)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/459823.html
