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

by John Norris

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