Why is my conditional validation failing?
I have a conditional validation in my Post model:
validates :title, :presence => true, :if => Proc.new { |post| post.post_type == "text" }
And I have the following spec in my post_spec.rb
file:
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 = P开发者_Go百科ost.new(@attr.merge(:title => "", :post_type => "image"))
post.should be_valid # This fails
end
My question is: Why does the second test fail?
You have to have some other validation somewhere failing. Go into your rails console and do:
post = Post.new(:title => "", :post_type => "image")
post.valid?
post.errors
and see what you get...
精彩评论