代码之家  ›  专栏  ›  技术社区  ›  simbro

无法将cy.server()与cypress cumber预处理器一起使用

  •  0
  • simbro  · 技术社区  · 7 年前

    当我试图运行cy.server()以模拟作为测试一部分执行的HTTP请求时,我得到以下错误:

    Uncaught CypressError: Cannot call "cy.server()" outside a running test.....

    我不知道如何让这个工作。这是我的代码:

    import { Given, Then } from 'cypress-cucumber-preprocessor/steps'
    
    beforeEach(() => {
      cy.server()
    
      cy.route({
        method: 'GET',
        url: '/',
        response: []
      })
    })
    
    const url = 'http://localhost:8080'
    
    Given('I click the big button', () => {
      cy.visit(url)
      cy.get('.btn').click()
    })
    
    Then('I can get the MOTD', (title) => {
      cy.title().should('include', title)
    })
    
    0 回复  |  直到 7 年前
        1
  •  1
  •   Mr. J.    7 年前

    您缺少Cypress的“it()”上下文。试试这个:

    import { Given, Then } from 'cypress-cucumber-preprocessor/steps'
    
      beforeEach(() => {
        cy.server()
    
        cy.route({
          method: 'GET',
          url: '/',
          response: []
        })
      })
    
      it('description of the it', function () {
        const url = 'http://localhost:8080'
    
        Given('I click the big button', () => {
          cy.visit(url)
          cy.get('.btn').click()
        })
    
        Then('I can get the MOTD', (title) => {
          cy.title().should('include', title)
        })
      })