我正在使用 Cypress 编写我的代码,并希望测试 Internet 故障或服务器未返回任何响应的情况。我正在查看 Cypress 文档,但没有发现任何有用的信息。我能找到的最接近的东西是
cy.route({
method: 'POST',
url: 'my_url',
response: {},
})
.as('validate');
问题是我无法将响应替换为未定义,因为 cypress 不允许这样做。如果我删除它,Cypress 将停止模拟我的 API。有帮助吗?
最佳答案
您可以使用“重定向”。
https://docs.cypress.io/api/commands/route.html#Simulate-a-server-redirect
describe('response will be empty', function()
{
it('Open web page',()=> {
let url = 'https://www.google.com/maps';
cy.server();
cy.route(
{
method: 'GET',
url: '**/maps/**',
//you can replace the status code you like
status: 503,
response: {
redirect: ''
}
}
);
cy.visit(url);
})
})
如果你使用
response: {}
它将返回空的响应体。
关于testing - Cypress - 虚假的互联网故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57985381/