无法将cy.server()与cypress-cucumber-preprocessor一起使用

时间:2019-04-02 10:39:21

标签: testing cucumber cypress

当我尝试运行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)
})

1 个答案:

答案 0 :(得分:1)

您缺少赛普拉斯的'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)
    })
  })