我正在运行两个测试,其中一个失败,一个通过。唯一的区别是使用:should
vs :expect
。为什么一个测试工作而另一个没测试?
通过考试:
it "returns no comma, when the integer is smaller than 1000" do
separate_comma(random_num(0, 999)).should match /^\d{1,3}$/
end
测试失败:
it "explanation" do
expect(separate_comma(random_num(0, 999))).to match /^\d{1,3}$/
end
这是无聊的事情:
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
答案 0 :(得分: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