Cy.intercept查询不适用于数组



在Cypress中使用查询参数模拟端点。但是当给查询字段一个数组的字段项。

cy.intercept(
{
method: 'POST',
pathname: '/api/initiate/manual',
hostname: Cypress.env('CY_API_URL'),
https: true,
query: {
orderId: '13535353453',
items: [
{
id: '3853438474',
name: 'Standard Wholesale HB Yellow Pencils',
},
{
id: '2424534353453',
name: 'natural wood dipped end 72 color',
},
],
},
},
{
statusCode: 200,
},
).as('mockManuelOrder');

给出错误:

CypressError

为cy.intercept()提供了一个无效的RouteMatcher。查询必须为字符串或正则表达式。

如何解决这个问题?我需要发送查询参数。

应该可以工作了:

query: JSON.stringify({
orderId: '13535353453',
items: [
{
id: '3853438474',
name: 'Standard Wholesale HB Yellow Pencils',
},
{
id: '2424534353453',
name: 'natural wood dipped end 72 color',
},
],
}),

这似乎可以工作,尽管我已经猜到了URL在通过网络发送时的样子(可能不对应)。

基本上,我已经将查询参数从RouteMatcher移动到RouteHandler中,并使用javascript来检查它们。

如果它们匹配,我分配一个动态别名并发送存根响应。

cy.intercept(
{
method: 'POST',
pathname: '/api/initiate/manual',
hostname: Cypress.env('CY_API_URL'),
https: true,
},
(req) => {
const params  = {
"orderId": "13535353453",
"items": [
{"id": "3853438474", "name": "Standard Wholesale HB Yellow Pencils"},
{"id": "2424534353453", "name": "natural wood dipped end 72 color"}
]
}
const matches = req.query.orderId === params.orderId &&
req.query.items == JSON.stringify(params.items)
if (matches) {
req.alias = 'mockManuelOrder'
req.reply({statusCode: 200})
}
}
// trigger POST
cy.wait('@mockManuelOrder')    // passes

最新更新