【问题标题】:Cypress: intercept and modify part of the responseCypress:拦截和修改部分响应
【发布时间】:2021-04-09 19:33:12
【问题描述】:

基于赛普拉斯docs,我想在第一次加载夹具后修改响应中的一个字段并保持其他所有内容不变。我知道我可以用 2 个灯具轻松做到这一点,但我不想为了简单的字段更改而复制它。我尝试了以下代码的变体,但没有成功。有什么想法吗?

  it('Should have the correct values in monthly', () => {
    cy.intercept('POST', `**/full`, (req) => {
      req.continue(res => {
        res.body.data.monthly = 5000;
        res.send(res);
      })
    });
    cy.fixture('calculator/monthlyRepayment.json').as('fixtures:monthlyRepayment');
    cy.route('POST', `**/full`, '@fixtures:monthlyRepayment').as(`request:fullData`);

    cy.get('[data-test="calculator:monthlyRepayment"]').should('contain', '$5000.00');
})

【问题讨论】:

  • 在你的例子中,你不是只存根一条路线吗?如果是这样,您能否修改您的 @fixtures:monthlyRepayment 以返回您想要的 data.monthly 值?
  • 另外,你为什么使用cy.interceptcy.routecy.routedeprecated,从 Cypress@6.0.0 开始。

标签: response cypress interceptor


【解决方案1】:

我发表了评论,但这也可以解决您的问题。你会想modify your fixture data before using it:

it('Should have the correct values in monthly', () => {
    cy.fixture('calculator/monthlyRepayment.json').then((json) => {
        json.monthly = 5000;
        cy.intercept('POST', '**/full', json);

        // cy.visit called somewhere here
        cy.get('[data-test="calculator:monthlyRepayment"]').should('contain', '$5000.00');
    });
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 2016-05-02
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多