reactjs - 赛普拉斯没有检测来自 cy.route() 的路由
问题描述
我已经准备好后端了。但我希望我的前端在测试时只使用我们可以在 cypress 中创建的模拟服务器。我的测试是
cy.server()
cy.route({
method:'POST',
url:'/dashboard/v1/public/login',
status:404,
response:{
"error": true,
"message": "User with above credentials does not exists"
}
})
cy.get('[data-testid="loging-page-email-input')
.type('test@gmail.com')
cy.get('[data-testid="loging-page-password-input')
.type('test123')
cy.get('[data-testid="loging-page-button-clicked')
.type('{enter}')
cy.get('[data-testid="loging-page-auth-error-msg') // Here it should grab alert box
但问题是,如果我们想物理登录网站,相同的凭据是正确的。因此,如果 cypress 能够登录,那么我们可以看到它没有获取我们提供的数据
cy.route({
method:'POST',
url:'/dashboard/v1/public/login',
status:404,
response:{
"error": true,
"message": "User with above credentials does not exists"
}
})
相反,它正在进行 api 调用并且能够登录。
知道出了什么问题吗?更多的是我的基本网址,http://localhost:3000
但我的完整登录 api 网址是y.x.in/dashboard/v1/public/login
更新 我现在也给了身体路线
cy.server()
cy.route({
method:'POST',
url:'/dashboard/v1/public/login',
body:{
email:"s@gmail.com",
password:"sss"
},
status:404,
response:{
"error": true,
"message": "User with above credentials does not exists"
}
})
我刚刚检查了单击登录按钮后,cypress 没有捕获登录 api 调用
解决方案
尝试这种方法,请注意method
,url
和response
不在options
参数范围内。
cy.server()
cy.route({
method:'POST',
url:'/dashboard/v1/public/login',
response: {
"error": true,
"message": "User with above credentials does not exists"
},
status:404,
options: {
body:{
email:"s@gmail.com",
password:"sss"
}
}
})
推荐阅读
- java - 在整个 Spring Boot Runtime 中存储 JSON 数据
- dd - 如何使用比原始磁盘更大的 dd 创建的映像?
- angular - 带有 loadchildren 的 Angular 延迟加载路由
- reactjs - Storybook + React:SVG 图标导致元素类型无效:需要字符串(用于内置组件)或类/函数
- javascript - 浏览器如何打开桌面应用程序?
- sql - 仅基于范围最小值的查找值
- cmake - CMake BISON_TARGET 防止生成标头
- sql - 如何使用新的 plpgsql 查看可用于访问的属性
- avro - Avro 工具失败预期启动联合。收到 VALUE_STRING
- python - 在同一行中删除特定单词及其后面的所有字符,并使用python从文本文件中删除下一行