gpt4 book ai didi

cypress - Cypress :想部分模拟XHR响应

转载 作者:行者123 更新时间:2023-12-03 10:07:14 34 4
gpt4 key购买 nike

我正在使用 Cypress ,并且想对XHR响应进行部分 stub 。我想捕获原始JSON,并对其进行部分编辑。
例如:cy.route('GET', `**/subjects`, 'fixture:mySubjects.json');这样,我就对整个响应进行了 stub ,但是我想看看:
原始XHR响应(当然还有许多其他属性):

{ 
'id': 12345,
"subjects": [
{
"key": "mat",
"name": "maths",
"hasAccess": true,
},
{
"key": "eng",
"name": "english",
"hasAccess": false,
}
],
}
我想 stub 的只是名字,并且想要得到:
{ 
'id': 12345,
"subjects": [
{
"key": "mat",
"name": "maths",
"hasAccess": true,
}
],
}
简而言之,我想做的是从响应中删除第二个主题“eng”。任何想法都非常感谢。

最佳答案

看看cy.intercept()
我不太了解您要返回或 stub 的实际响应的哪一部分,但这是这样做的机制。

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()

关于cypress - Cypress :想部分模拟XHR响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66276942/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com