Cypress:拦截并修改部分响应

时间:2021-04-09 19:33:12

标签: response cypress interceptor

基于 Cypress 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');
})

1 个答案:

答案 0 :(得分:0)

我发表了评论,但这也可以解决您的问题。你会想要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');
    });
})