什么是Rspec 3.0匹配器=〜?

时间:2014-06-19 20:05:41

标签: ruby rspec rspec3

说你有:

Article.keys.should =~ [:title, :year, :title_length]

在Rspec 3.0中成为

expect(Article.keys).to =~ [:title, :year, :title_length]

除非失败:

ArgumentError:
   The expect syntax does not support operator matchers, so you must pass a matcher to `#to`.

关键问题=~运算符在Rspec 3.0中的匹配器是什么?

2 个答案:

答案 0 :(得分:4)

expect([1, 2, 3]).to match_array([2, 1, 3])

我认为这就是你要找的东西

答案 1 :(得分:2)

值得一提的是Rspec 3.0 Collection membership

expect(array).to match_array(expected_array)
# ...which is the same as:
expect(array).to contain_exactly(individual elements)

doc中的示例: -

expect([1, 2, 3]).to contain_exactly(2, 1, 3)
# is exactly same as below
expect([1, 2, 3]).to match_array([3, 2, 1])