views:

276

answers:

1

I often want to compare arrays and make sure that they contain the same elements, in any order. IS there a consise way to do this in RSpec?

Here are methods that aren't acceptable:

#to_set

For example:

array.to_set.should == another_array.to_set

This fails when the arrays contain duplicate items.

#sort

For example:

array.sort.should == another_array.sort

This fails when the arrays elements don't implement #<=>

#size and #to_set

For example:

array.to_set.should == another_array.to_set
array.size.should == another_array.size

This would work, but there's got to be a better way.

+5  A: 

Try array.should =~ another_array

The best documentation on this I can find is the code itself, which is here.

x1a4
nice, very useful!
severin