[cypress]如何检查请求尚未发送



我需要检查在第二次单击同一按钮后,不发送HTTP请求。有办法吗?我已经尝试过拦截和等待这个目的,但不能使它工作

一次性拦截可能会起作用

const interceptOnce = (method, url, response) => {
  let count = 0
  return cy.intercept(method, url, req => {
    count += 1
    if (count < 2) {
      req.reply(response)
    } else {
      throw 'Error: button click caused two requests'    // cause test to fail
    }
  })
}
it('tests that two button clicks only sends a request on first click', () => {
  interceptOnce('POST', myurl, {stubbed response object})
  cy.get('button').click()
  cy.get('button').click()    // test fails here if a second request occurs
}) 

我不确定在这种情况下是否需要存根响应。

最新更新