我正在使用 Cypress,我想部分存根 XHR 响应。我想捕捉原始 JSON,并对其进行部分编辑。
例如:
cy.route('GET', `**/subjects`, 'fixture:mySubjects.json');
这样我就截断了整个回复,但我想看看:
原始 XHR 响应(当然还有许多其他属性):
{
'id': 12345,
"subjects": [
{
"key": "mat",
"name": "maths",
"hasAccess": true,
},
{
"key": "eng",
"name": "english",
"hasAccess": false,
}
],
}
我想存根的只是名字,想得到:
{
'id': 12345,
"subjects": [
{
"key": "mat",
"name": "maths",
"hasAccess": true,
}
],
}
简而言之,我想做的是从响应中删除第二个主题“eng”。任何想法都非常感谢。
答案 0 :(得分:1)
我不太明白您想要返回或存根真实响应的哪些部分,但这是这样做的机制。
cy.intercept('/integrations', (req) => {
// req.reply() with a callback will send the request to the destination server
req.reply((res) => {
// 'res' represents the real destination response
// you can manipulate 'res' before it's sent to the browser
})
})
如果您使用的是 Cypress 版本 < 6,您可以尝试使用具有相同语法的 cy.route2()
。