Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

136
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda