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

前端 未结 6 2044

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 are

6条回答
  •  青春惊慌失措
    2020-12-12 10:35

    Since RSpec 2.11 you can also use match_array.

    array.should match_array(another_array)
    

    Which could be more readable in some cases.

    [1, 2, 3].should =~ [2, 3, 1]
    # vs
    [1, 2, 3].should match_array([2, 3, 1])
    

提交回复
热议问题