Ruby on rails Rspec:should vs:expect与regex
我正在运行两个测试,其中一个失败,另一个通过。唯一的区别是使用了Ruby on rails Rspec:should vs:expect与regex,ruby-on-rails,regex,rspec,Ruby On Rails,Regex,Rspec,我正在运行两个测试,其中一个失败,另一个通过。唯一的区别是使用了:should和: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
:should
和: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
这不是一个答案,而是一个相关的问题。以下规格通过,基本内容复制自OP的代码。有人能解释为什么OP的规范在
expect
情况下会失败,为什么正则表达式周围的括号会起作用?(注意:我使用的是Ruby 2.0和RSpec 2.14)
如果正则表达式在parens中,它有效吗?测试的结果是什么?太棒了。成功了,谢谢!
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