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

时间:2021-02-19 11:41:20

标签: cypress stubbing cypress-cucumber-preprocessor

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

1 个答案:

答案 0 :(得分:1)

看看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 版本 < 6,您可以尝试使用具有相同语法的 cy.route2()