我的 Post 模型中有一个条件验证:
validates :title, :presence => true, :if => Proc.new { |post| post.post_type == "text" }
我的 post_spec.rb
文件中有以下规范:
it "should only require a title if the post type is text" do
post = Post.new(@attr.merge(:title => "", :post_type => "text"))
post.should_not be_valid
post = Post.new(@attr.merge(:title => "", :post_type => "image"))
post.should be_valid # This fails
end
我的问题是:为什么第二次测试失败了?
最佳答案
您必须在某个地方进行其他验证失败。进入你的 Rails 控制台并执行:
post = Post.new(:title => "", :post_type => "image")
post.valid?
post.errors
看看你得到了什么......
关于ruby-on-rails - 为什么我的条件验证失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7455876/