【问题标题】:Cypress: want to partially mock XHR responseCypress:想要部分模拟 XHR 响应
【发布时间】:2021-05-22 09:46:52
【问题描述】:

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

【问题讨论】:

    标签: cypress stubbing cypress-cucumber-preprocessor


    【解决方案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
      })
    })
    

    如果您使用的是赛普拉斯版本 cy.route2()。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多