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

167
Visualizações
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 Respostas
Responde à pergunta

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 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