1

I am running two tests where one fails and one passes. The only difference is the use of :should vs :expect. Why does one test work and the other doesn't?

Passing Test:

it "returns no comma, when the integer is smaller than 1000" do
  separate_comma(random_num(0, 999)).should match /^\d{1,3}$/
end

Failing Test:

it "explanation" do
  expect(separate_comma(random_num(0, 999))).to match /^\d{1,3}$/
end

Here's the boring stuff:

def random_num(min, max)
   rand(max - min + 1) + min
end

def separate_comma(number, delimiter = ',')
  new = number.to_s.reverse.scan(/.../).join(delimiter)
end
4

1 回答 1

3

这不是一个答案,而是一个相关的问题。以下规范通过,从 OP 代码复制的基本内容。有人可以解释为什么 OP 的规范会因这种expect情况而失败,以及为什么正则表达式周围的括号会有所不同吗?(注意:我使用的是 Ruby 2.0 和 RSpec 2.14)

def random_num(min, max)
   rand(max - min + 1) + min
end

def separate_comma(number, deliminator = ',')
  new = number.to_s.reverse.scan(/.../).join(deliminator)
end

describe "rspec expectations involving match, regex and no parentheses" do

  it "works for should" do
    separate_comma(random_num(0, 999)).should match /^\d{1,3}$/
  end

  it "works for expect" do
    expect(separate_comma(random_num(0, 999))).to match /^\d{1,3}$/
  end

end
于 2013-11-15T06:49:51.513 回答