You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Cypress的cy.intercept无法验证响应体中maximum_quantity字段

Cypress拦截请求后无法校验响应中maximum_quantity属性的问题

问题说明

前端编辑字段并保存后,通过cy.intercept拦截指定GET请求,试图校验响应中的maximum_quantity属性是否已更新为30,但无法定位到该属性完成校验。

现有代码

cy.intercept('GET', '***/all-offerings?page=0&size=15&not_status=deactivated',).as('max')

cy.wait('@max')
  .its("response.statusCode")
  .should("eq", 200)

cy.get('@max')
  .then(res => {
    expect(res.data[0].items[0].maximum_quantity).to.have.value(30)
})

错误信息

错误信息

JSON响应结构

JSON响应

问题分析及修正代码

错误点

  1. 响应数据路径错误:Cypress拦截的请求响应数据存储在interception.response.body中,而非res.data
  2. 断言方法错误:to.have.value()用于校验DOM元素的value属性,普通JS属性应使用to.eq()
  3. 层级路径错误:从响应截图看,数据实际层级为response.body.content而非response.data

修正后的代码

cy.intercept('GET', '***/all-offerings?page=0&size=15&not_status=deactivated').as('max')

cy.wait('@max')
  .its("response.statusCode")
  .should("eq", 200)

cy.get('@max')
  .then((interception) => {
    expect(interception.response.body.content[0].items[0].maximum_quantity).to.eq(30)
})

内容的提问来源于stack exchange,提问作者Kakashi - Sensei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 01:38:19