Rspec array equality
[1,2,3].should =~ [1,3,2]
is the same as
[1,2,3].sort.should == [1,3,2].sort
If you need to test whether two arrays contain same elements disregarding its order, you could use "=~" method.
Written by Dmitry Novotochinov
Related protips
4 Responses
Superb! Just what i missed for so long.
over 1 year ago
·
"If you need to test whether two arrays contain same elements disregarding its order"
In such a case you should you sets. ;)
over 1 year ago
·
@cypok yes, but only in case you have no duplicates.
over 1 year ago
·
I prefer 'match_array' instead.
http://rubydoc.info/github/rspec/rspec-expectations/RSpec/Matchers:match_array
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Array
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#