【发布时间】:2020-04-16 16:34:15
【问题描述】:
我正在尝试在使用 cypress 编写的一些 UI 测试中模拟服务器。我可能犯了一些基本错误,并且可能不了解赛普拉斯如何存根请求。这是我直接从 expressjs 复制的示例应用程序 -
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello from /'));
app.get('/user', (req, res) => res.send('Hello from /user'));
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
然后用cypress写了一个简单的测试——
describe('Stubbed request', () => {
it('sends whatever response you want', () => {
cy.visit('http://localhost:3000/');
cy.server();
cy.route({
method: 'GET',
url: '/user',
response: [],
}).as('bar');
cy.visit('http://localhost:3000/user'); // cy.request() has same behavior
cy.wait('@bar');
})
})
我希望得到的不是“来自用户/的你好”,而是得到一个空的响应,因为我已经用 cypress 将它存根了。甚至 cy.wait 也失败并显示消息 -“CypressError:重试超时:cy.wait() 超时等待 5000 毫秒,等待对路由的第一个请求:'bar'。没有发生任何请求。”我显然做错了什么。有人可以帮我理解我做错了什么吗?提前致谢。
【问题讨论】:
标签: cypress