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

165
Vistas
RSpec match_array with hashes

I was wondering if there's a way to achieve what I want with RSpec. So I want to test an array of hashes that looks similar to this one:

array_example =
 [
  {
   id: 1, 
   link: "www.some-link.com/path_im_not_interested_in"
  }, 
  {
   id: 2, 
   link: "www.example-link.com/another_useless_path"
  }
 ]

I want to do integration test in which I won't include those paths in links. I'm only interested in checking if www.some-link.com and www.example-link.com are present in those hashes in array. Something like this:

expect(array_example).to match_array(
 [
  {
   id: 1, 
   link: "www.some-link.com"
  }, 
  {
   id: 2, 
   link: "www.example-link.com"
  }
 ]

But this one obviously won't work. Is there a way to achieve what I want? I would like to avoid using custom matchers.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can use composed matchers:

expect(array_example)
  .to contain_exactly(
    a_hash_including(id: 1, link: a_string_including("www.some-link.com")),
    a_hash_including(id: 2, link: a_string_including("www.example-link.com"))
  )

See https://rspec.info/blog/2014/01/new-in-rspec-3-composable-matchers/ for more information.

The argument of a_hash_including does not have to cover the whole hash, it will only check the keys you are passing.

over 4 years ago · Santiago Trujillo 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