赛普拉斯:想部分模拟 XHR 响应

问题描述

我正在使用 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,}

简而言之,我想做的是从响应中删除第二个主题“eng”。任何想法都非常感谢。

解决方法

看看cy.intercept()

我不太明白您想要返回或存根真实响应的哪些部分,但这是这样做的机制。

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 版本 cy.route2()。