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

Cypress: want to partially mock XHR response

我正在使用 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'。非常感谢任何想法。

看看cy.intercept()

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

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

如果您使用的是低于 6 的 Cypress 版本,您可以尝试使用具有相同语法的 cy.route2()