1

How can I intercept the entire requestbody and compare this with an fixture?

I tried:

it('Check if Frontend requestbody is the correct', () => {
    
    cy.intercept('GET', '**/api/assessmenttestreference').as('ItemMove')
    cy.wait('@ItemMove').its('request.body')
        .to.equal({fixture:'MovedItemStructure.json'})

});

Is there a working or an alternative solution for this goal?

TesterDick
  • 3,830
  • 5
  • 18
E. E. Ozkan
  • 149
  • 10

3 Answers3

2

To check it against a fixture you will have to use deep.equal.

const movedItemStructure = require('/pathToFixtures/MovedItemStructure.json')

it('Check if Frontend requestbody is the correct', () => {
    cy.intercept('GET', '**/api/assessmenttestreference').as('ItemMove')
    // action to trigger request
    cy.wait('@ItemMove').its('request.body')
        .should('deep.equal', movedItemStructure)

});

jjhelguero
  • 2,281
  • 5
  • 13
2

Use a routeHandler function, which gives you access to all properties of the request.

cy.fixture('MovedItemStructure.json').then(fixture => {
  cy.intercept('GET', '**/api/assessmenttestreference', (req) => {  
    expect(req.body).to.deep.eq(fixture)
  })
  .as('ItemMove')
})

If you want to compare the response body, you can nest another callback

cy.fixture('MovedItemStructure.json').then(fixture => {
  cy.intercept('GET', '**/api/assessmenttestreference', (req) => {  

    req.continue((res) => {
      expect(res.body).to.deep.eq(fixture)
    })
  })
  .as('ItemMove')
})
TesterDick
  • 3,830
  • 5
  • 18
1

Thanks to TesterDick. I have combined his answers to the following

    cy.fixture('MovedItemStructure.json').then(fixture => {
        cy.intercept('PUT', '**/api/assessmenttest/*', (req) => {
            expect(req.body).to.deep.eq(fixture)
            req.reply({ fixture: 'MovedItemStructure.json' })
        }).as('ItemMove')
    })
E. E. Ozkan
  • 149
  • 10