Cypress - cy.wait() 超时等待 5000 毫秒以等待对路由的第一个请求

Ben*_*wer 7 cypress

Cypress 的新手,到目前为止一直很顺利,直到我想伪造一个网络请求。代码是这样的:

describe('Some test', function(){
it('Can fake out an XHR response', function(){
    cy.server()
    cy.route('https://reqres.in/api/users', [{ id: 1, name: 'Pat' }]).as('getMessages')
    cy.request('https://reqres.in/api/users')
    cy.wait(['@getMessages'])
Run Code Online (Sandbox Code Playgroud)

...

无论我尝试什么,我都会得到:

CypressError:重试超时:cy.wait() 等待 5000 毫秒超时,等待对路由的第一个请求:'getMessages'。从未发生过请求。

我在这里做错了什么?

编辑:我将代码更改为:

cy.server()
cy.route('https://reqres.in/api/users', [{ id: 1, name: 'Pat' }]).as('getMessages')
cy.request('https://reqres.in/api/users')
cy.wait(['@getMessages'])
Run Code Online (Sandbox Code Playgroud)

但结果如下:

Seb*_*arz 1

从 Cypress 6.0.0 开始,cy.intercept()是cy.route()的后继者,可用于返回预期响应,如下所示:

cy.intercept('GET','/api/users', expectedResult).as('interceptedRequest');

cy.wait('@interceptedRequest');
Run Code Online (Sandbox Code Playgroud)

https://docs.cypress.io/api/commands/intercept