Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cypress 断言请求';没有发生_Cypress - Fatal编程技术网

Cypress 断言请求';没有发生

Cypress 断言请求';没有发生,cypress,Cypress,我有以下测试 it('does not trigger request', () => { cy.shortcutThatTriggersRequest() cy.route({ method: 'POST', url: '[url]', onRequest: () => { cy.contains('request was invoked').should('exist') }, })

我有以下测试

it('does not trigger request', () => {
    cy.shortcutThatTriggersRequest()

    cy.route({
      method: 'POST',
      url: '[url]',
      onRequest: () => {
        cy.contains('request was invoked').should('exist')
      },
    })

    cy.wait(1000)
})
这就是我想要做的——如果给定的请求发生了,它将无法通过测试,如果没有,那么测试将通过

不过,它有两个麻烦的地方:

  • 我发现,
    Cypress检测到您在调用某个命令中的一个或多个cy命令时,从该命令返回了一个承诺。
    ;我不能忽略
    wait
    ,否则测试将被标记为通过(在解决请求时测试结束,测试被标记为通过,然后失败);只有在发出请求时才会发生这种情况(因为
    路由
    没有被触发,所以没有
    包含
    可以在
    等待
    的同一点触发)
  • 我依靠
    cy.contains('request was called').should('exist')
    将测试标记为失败。我不想那样做;虽然这样的字符串永远不会存在,但它是一种变通方法;理想情况下,我希望co
    cy.fail()
    ,但我看不到这种方法

    • 好的,为了实现这一点,我使用了以下方法:

      it.skip('does not trigger products requests', () => {
          Cypress.on('fail', (error, runnable) => {
            if (error.message.indexOf('Timed out retrying') !== 0) throw error
          })
      
          cy.server()
          cy.route({
            method: 'POST',
            url: '[url]',
          }).as('products')
      
          cy.shortcutThatTriggersRequest()
          cy.wait('@products', {
            requestTimeout: 1000,
          }).then((xhr) => {
            assert.isNull(xhr.response.body)
          })
      })
      
      如果达到超时,它将在
      失败
      内处理,因此测试将通过。如果它进入
      内部,则测试将失败

      这里还有一个问题,即如果检查了多个路由(
      cy.wait['route1',route2']
      ),但其中一个路由不存在,那么即使其中一个断言指定它不应该存在,测试也会通过,例如:

      it.skip('does not trigger products requests', () => {
          Cypress.on('fail', (error, runnable) => {
            if (error.message.indexOf('Timed out retrying') !== 0) throw error
          })
      
          cy.server()
          cy.route({
            method: 'POST',
            url: '[url]',
          }).as('route1')
      
          cy.route({
            method: 'POST',
            url: '[url]',
          }).as('route2')
      
          cy.shortcutThatTriggersRequest()
          cy.wait(['@route1', '@route2'], {
            requestTimeout: 1000,
          }).then((xhrs) => {
            assert.isNull(xhrs[0].response.body)
            assert.isNull(xhrs[1].response.body)
          })
      })
      
      即使有对其中一个端点的请求(我假设这是由处理
      onfail
      引起的错误)。需要做的是,请求断言必须是连续的:

      cy.wait('@route1', {
        requestTimeout: 1000,
      }).then((xhr) => {
        assert.isNull(xhr.response.body)
      })
      
      cy.wait('@route2', {
        requestTimeout: 1000,
      }).then((xhr) => {
        assert.isNull(xhr.response.body)
      })
      

      据我所知,我们无法在
      onfail
      中检查
      error
      的类型,因为它总是
      CypressError
      不管是什么原因造成的,好的,为了实现这一点,我使用了以下方法:

      it.skip('does not trigger products requests', () => {
          Cypress.on('fail', (error, runnable) => {
            if (error.message.indexOf('Timed out retrying') !== 0) throw error
          })
      
          cy.server()
          cy.route({
            method: 'POST',
            url: '[url]',
          }).as('products')
      
          cy.shortcutThatTriggersRequest()
          cy.wait('@products', {
            requestTimeout: 1000,
          }).then((xhr) => {
            assert.isNull(xhr.response.body)
          })
      })
      
      如果达到超时,它将在
      失败
      内处理,因此测试将通过。如果它在
      内,则
      内,则测试将失败

      这里还有一个问题,即如果检查了多个路由(
      cy.wait['route1',route2']
      ),但其中一个路由不存在,那么即使其中一个断言指定它不应该存在,测试也会通过,例如:

      it.skip('does not trigger products requests', () => {
          Cypress.on('fail', (error, runnable) => {
            if (error.message.indexOf('Timed out retrying') !== 0) throw error
          })
      
          cy.server()
          cy.route({
            method: 'POST',
            url: '[url]',
          }).as('route1')
      
          cy.route({
            method: 'POST',
            url: '[url]',
          }).as('route2')
      
          cy.shortcutThatTriggersRequest()
          cy.wait(['@route1', '@route2'], {
            requestTimeout: 1000,
          }).then((xhrs) => {
            assert.isNull(xhrs[0].response.body)
            assert.isNull(xhrs[1].response.body)
          })
      })
      
      即使有对其中一个端点的请求(我假设这是由处理
      onfail
      引起的错误)。需要做的是,请求断言必须是连续的:

      cy.wait('@route1', {
        requestTimeout: 1000,
      }).then((xhr) => {
        assert.isNull(xhr.response.body)
      })
      
      cy.wait('@route2', {
        requestTimeout: 1000,
      }).then((xhr) => {
        assert.isNull(xhr.response.body)
      })
      
      据我所知,我们无法在
      onfail
      中检查
      error
      的类型,因为它总是
      CypressError
      而不管是什么原因造成的