简体   繁体   English

Rspec验证测试(Mongoid)

[英]Rspec Validation testing (Mongoid )

I got a problem when i testing validations in rails with mongoid This is for example my Person Model and my Rspec test. 我在使用mongoid测试rails中的验证时遇到了问题这是我的人员模型和我的Rspec测试。

class Person
    include Mongoid::Document

    validates :first_name , :presence => true
    validates :last_name , :presence => true

end



 [ :last_name, :first_name ].each do |attr|
  it "must have a #{attr}" do
    p = Person.new
    p.send("#{attr}=","")
    p.should_not be_valid
    p.errors[attr].should == [ "can't be blank" ] 
  end
end

But this tests fail because the return value is ["can't be blank", "can't be blank"] 但是这个测试失败了,因为返回值是[“不能为空”,“不能为空”]

  expected: ["can't be blank"] got: ["can't be blank", "can't be blank"] 

Why are 2 Errors in this record?? 为什么这个记录中有2个错误? i test this issue with many models and other validation rules. 我用许多模型和其他验证规则测试这个问题。 I got everytime the same result 我每次都得到相同的结果

Thx for help 谢谢你的帮助

Eric 埃里克

It's because you're setting one of your fields to empty string and the other retains default value of nil . 这是因为您将一个字段设置为空字符串而另一个字段保留默认值nil That's why both of your validations fail each time. 这就是为什么每次验证都失败的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM