A great way of testing a collection in RSpec without needing to care about the order.

I had a test failing because the method I was checking to have received some IDs had indeed received the IDs, but in the wrong order:

expect(InviteUsersJob).to have_received(:perform_async).with(users.map(&:id))

Expect: [1,2,3,4,5]
Actual: [2,1,4,5,3]

It was a service that was going to email all of the users with those IDs, so I don’t care what the order is.

I can’t sort both sides, because one side is within the service I’m testing, and I’m not sure that sorting the IDs in the service just to make testing easier is great practice.

Fortunately, you can use a method called a_collection_containing_exactly

expect(InviteUsersJob).to have_received(:perform_async).with(a_collection_containing_exactly(*users.map(&:id)))

John Norris


Who are Dragon Drop?

Dragon Drop is a specialist web and app development agency. The team has extensive full-stack technical credentials and a strong focus on user experience.

The Dragon Drop founding team have over 40 years of web development and project experience. They have managed or developed over 100 significant ecommerce and web projects during their careers, including implementations for major UK high street retailers, financial services companies and government agencies.

Their approach to innovative solutions stems from perspectives gained as retailer, software supplier and web agency.