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.
CodePudding user response:
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.