我经常想比较数组并确保它们以任何顺序包含相同的元素。在 RSpec 中是否有一种简洁的方法来做到这一点?
以下是不可接受的方法:
#设置
例如:
expect(array.to_set).to eq another_array.to_set
或者
array.to_set.should == another_array.to_set
当数组包含重复项时,这会失败。
#种类
例如:
expect(array.sort).to eq another_array.sort
或者
array.sort.should == another_array.sort
当数组元素未实现 #<=>
时,此操作失败
to_set
和 size
实际上并没有达到你想要的效果。例如 [a, b, b] 将匹配 [a, a, b]。干杯!
eq
匹配器,例如 expect([1, 2]).to_not eq([2, 1])
从 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])
eq()
,所以我想 match_array()
对我来说已经足够模糊了。
我发现 =~
是不可预测的,并且它没有明显的原因失败。过去 2.14,您可能应该使用
expect([1, 2, 3]).to match_array([2, 3, 1])
expect([1, 2, 3]).to contain_exactly(2, 3, 1)
。 relishapp.com/rspec/rspec-expectations/v/3-0/docs/…
使用 match_array
,将另一个数组作为参数,或 contain_exactly
,将每个元素作为单独的参数,有时对可读性很有用。 (docs)
例子:
expect([1, 2, 3]).to match_array [3, 2, 1]
或者
expect([1, 2, 3]).to contain_exactly 3, 2, 1
对于 RSpec 3,使用 contain_exactly
:
有关详细信息,请参阅 https://relishapp.com/rspec/rspec-expectations/v/3-2/docs/built-in-matchers/contain-exactly-matcher,但这里是摘录:
contains_exactly 匹配器提供了一种测试数组的方法,这种方法忽略了实际数组和预期数组之间的排序差异。例如:expect([1, 2, 3]).to contains_exactly(2, 3, 1) # 通过 expect([:a, :c, :b]).to contains_exactly(:a, :c ) # 失败
正如其他人指出的那样,如果您想断言相反的情况,即数组应同时匹配内容和顺序,则使用 eq
,即:
expect([1, 2, 3]).to eq([1, 2, 3]) # pass
expect([1, 2, 3]).to eq([2, 3, 1]) # fail
没有很好地记录,但我还是添加了链接:
Rspec3 文档
expect(actual).to eq(expected)
Rspec2 文档
expect([1, 2, 3]).to match_array([2, 3, 1])
.to eq
方法,而不是 match_array
。
不定期副业成功案例分享
Passes if actual contains all of the expected regardless of order.
。expect([1, 2, 3]).to match_array([2, 1, 3])
请参阅:stackoverflow.com/a/19436763/33226