Rspec:“array.should == another_array”,但没有关心顺序
我经常想要比较数组,并确保它们以任何顺序包含相同的元素。 在RSpec中有一个简洁的方法吗?
以下是不可接受的方法:
 #to_set 
例如:
 array.to_set.should == another_array.to_set 
当数组包含重复的项目时失败。
 #sort 
例如:
 array.sort.should == another_array.sort 
 当数组元素不执行#<=>时失败 
 尝试array.should =~ another_array 
关于这个我能find的最好的文档是代码本身,这是在这里 。
 既然RSpec 2.11,你也可以使用match_array 。 
 array.should match_array(another_array) 
在某些情况下,这可能更具可读性。
 [1, 2, 3].should =~ [2, 3, 1] # vs [1, 2, 3].should match_array([2, 3, 1]) 
 我发现=~是不可预知的,并且没有明显的原因失败了。 过去的2.14,你应该可以使用 
 expect([1, 2, 3]).to match_array([2, 3, 1]) 
没有logging得很好,但我反正添加了链接:
Rspec3 文档
 expect(actual).to eq(expected) 
Rspec2 文档
 expect([1, 2, 3]).to match_array([2, 3, 1])