【问题标题】:Cypress testing and waiting for axios requestsCypress 测试并等待 axios 请求
【发布时间】:2020-12-03 22:03:45
【问题描述】:

谁能解释我的测试在继续之前如何或为什么不等待来自 axios 请求的数据?我对此完全陌生,但已经解决了最简单的问题,但似乎无法浏览文档以找到我出错的地方。 这是相关信息..

cy.get('.day').eq(4).click() //Change the day
cy.route('/api/practice/available-slots').as('apiCheck') //Get available slots for that day
cy.wait('@apiCheck') //Wait for the days available slots to be returned

所以你可以在下面看到我点击第四天,我的帖子 URL 正在显示并像往常一样获取数据,但随后我的等待函数抛出了该错误。我喜欢认为我很接近,但正如我所说,我是新手,并不完全确定出了什么问题。谢谢

这里的 axios 请求值得:

axios
    .post(this.props.reqProto + this.props.reqHost + '/api/practice/available-slots', {
      startDate: this.state.appointmentSlotsDate,
    })
    .then((res) => {
          ....
     }
 })

【问题讨论】:

  • cy.route() "如果没有定义方法,赛普拉斯将默认匹配 GET 请求",但您的 xhr 是 'POST',所以将方法添加为第一个参数。

标签: reactjs automated-tests cypress


【解决方案1】:

谢谢希拉姆,

这是一个问题。我的代码顺序也不正确。我需要允许 cypress 预测 POST 请求,而不是尝试加倍返回它。这似乎有效

cy.route({
  method: 'POST',
  url: '/api/practice/available-slots',
}).as('apiCheck')

cy.get('.day').eq(4).click()
cy.wait('@apiCheck')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-01
    • 1970-01-01
    • 2020-06-08
    • 2021-05-04
    • 2019-07-08
    • 2020-02-08
    • 2019-04-02
    • 1970-01-01
    相关资源
    最近更新 更多