用rspec和factory_girl测试变量字符串

时间:2014-02-23 16:34:30

标签: ruby-on-rails rspec factory-bot rspec-rails

我想用rspec和factory_girl测试模型属性的验证。 “特殊”的是,一个属性(名称)不允许以数字或特殊符号开头,如%,&,$,...

为了测试它,只编写一个像

这样的例子会很棒
it "is invalid with name starting by special character" do
    ["1","$","%"].each do |i|
       name = i + "test"
       FactoryGirl.build(:tutu, name: name).should_not be_valid
    end
end 

这项工作适用于第一种情况,但不会返回整个列表的结果。是否有可能告诉rspec不要停止错误?

1 个答案:

答案 0 :(得分:4)

请改为:

["1","$","%"].each do |i|
  it "is invalid with name starting by '#{i}'" do
    FactoryGirl.build(:tutu, name: "#{i}test").should_not be_valid
  end
end