[英]Cypress - cy.intercept catching wrong url
我尝试用intercept
来模拟我的 API 路线,但我不知道为什么触发错误的路线(我在 cypress@6.2.1 上)
我有两个拦截:
一个用于/contacts
,第二个用于 / /contacts/Contact-ARandomId
cy.intercept('GET', 'http://localhost:5000/contacts', {statusCode: 200, body: dataMultiple})
cy.intercept('GET', 'http://localhost:5000/contacts/Contact-ARandomId', {statusCode: 200, body: dataARandomId})
参考匹配 URL
您可以提供URL的 substring 来匹配
// 将匹配任何包含“用户”ZE83AED3DDF4667DEC0DAAAACB2BB3BE0BZ 的请求,例如
// GET /users?_limit=3 和 POST /userscy.intercept('用户')
所以'http://localhost:5000/contacts'
匹配,因为它是第一个定义的,并且适用部分匹配。
您可以颠倒拦截的顺序,首先设置更具体的 URL (有点像 SPA 上的路线)。
或者,查看动态设置别名。
您可以使用 javascript 来细化响应
cy.intercept('GET', 'http://localhost:5000/contacts', (req) => {
const isContactById = req.url.split('/') // split into parts
.pop() // take last part
.startsWith('Contact-'); // check if has id prefix
const bodyStub = isContactById ? dataARandomId : dataMultiple;
req.reply(200, bodyStub);
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.