【发布时间】:2026-01-30 03:05:01
【问题描述】:
我使用Cypress 作为 API 和 UI 测试的自动化框架。我编写了多个正在运行和通过的 API 测试,但它们只是验证 response.status 返回 200。我想将来自 GET 的响应 json 与存储的“预期”响应进行比较,以确认 JSON 响应数据是否正确。
我在.then(response => {} 代码块中尝试了to.deep.equal 和deepEquals 的不同变体。但我不想验证只有一个字段返回正确的值,我想验证一堆不同的字段是否返回正确的值。我的GET 请求返回超过 100 行嵌套的 JSON 字段/值,我只想验证嵌套在彼此内部的 20 个左右的字段/值。
cy.request({
method: 'GET',
log: true,
url: 'https://dev.api.random.com/calculators/run-calculate/524/ABC',
headers: {
'content-type': 'application/json',
'x-api-key': calcXApiKey
},
body: {}
}).then(response => {
const respGet = response.body
const tPrice = response.body.data.calculations.t_costs[0].comparison.t_price
cy.log(respGet, tPrice)
assert.deepEqual({
tPrice
}, {
t_price: '359701'
})
// assert.equal(response.status, 200) -- This works great
})
错误=expected { tPrice: undefined } to deeply equal { t_price: 359701 }
【问题讨论】:
-
对于任何未来的搜索者,除了
deepEqual之外,还有一个叫做deepInclude的东西
标签: cypress chai web-api-testing