Rspec: "array.should == another_array" but without concern for order

Posted by nicholaides on Stack Overflow See other posts from Stack Overflow or by nicholaides
Published on 2010-06-05T02:50:00Z Indexed on 2010/06/05 7:32 UTC
Read the original article Hit count: 166

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about testing