Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!