I often want to compare arrays and make sure that they contain the same elements, in any order. Is there a concise way to do this in RSpec?
Here are methods that aren't acceptable:
#to_set
For example:
expect(array.to_set).to eq another_array.to_set
or
array.to_set.should == another_array.to_set
This fails when the arrays contain duplicate items.
#sort
For example:
expect(array.sort).to eq another_array.sort
or
array.sort.should == another_array.sort
This fails when the arrays elements don't implement #<=>
to_set
andsize
actually doesn't do what you want. E.g. [a, b, b] would match [a, a, b]. Cheers! – Jo Lisseq
matcher, e.g.expect([1, 2]).to_not eq([2, 1])
– Dennis