Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
Cypress POST request, how to access 'fields' section of reponse body

I want to send a POST request in Cypress that triggers the validation to reject the request

According to Postman, the body of the response looke like this:

    "code": "validation_error",
    "message": "Validation error, please see fields property for details",
    "fields": 
    {
        "TariffData[rate_gp]": " Invalid rate_gp. Expected: 9.35. Imported: 19.35"
    }

I am interested in the "fields" section, so I tried to assert with this code:

const api_key = require('../../fixtures/contracts/api_test_client1.json')
const body1 = require('../../fixtures/contracts/body_test_client1.json')
describe('test POST/client validation', () => {

  it('send POST/client request', function () {
        cy.request({
              method: 'POST',
              url: Cypress.env('staging_url') + '/service/clients',
              headers: {
                       'API-KEY': api_key,
                       },
              body:    body1,
              failOnStatusCode:false
                  })
            .then(response => {
                expect(response.status).to.eq(400)
                expect(response.body.fields).to.contain('"TariffData[rate_gp]": " Invalid rate_gp. Expected: 9.35. Imported: 19.35"')
                    })
  )}
)}

Yet this leads to an error:

AssertionError

object tested must be an array, a map, an object, a set, a string, or a weakset, but object given

Yes, the error message ends there. Any ideas how I can make an assertion that the response contains this message?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think you just want to present the expected value as an object, not as a string

  expect(response.body.fields)
    .to.contain({
      "TariffData[rate_gp]": " Invalid rate_gp. Expected: 9.35. Imported: 19.35"
    })

If you looks at the docs chaijs API they show, for example

expect({a: 1, b: 2, c: 3}).to.include({a: 1, b: 2});

and contain is a synonym for include

You could also try to.deep.equal, as it seems you are specifying the total fields property

  expect(response.body.fields)
    .to.deep.eq({
      "TariffData[rate_gp]": " Invalid rate_gp. Expected: 9.35. Imported: 19.35"
    })
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda