[rspec-users] [rails] shoud I test validate_presentence_of series?

Phillip Koebbe phillipkoebbe at gmail.com
Wed Jul 21 09:08:48 EDT 2010

  On 2010-07-21 1:41 AM, Zhenning Guan wrote:
> Suppose I have a model Forum, have some attributes, title, content,
> tag.so I do it in Forum model.
> validates_presence_of :title
> validates_presence_of :tag
> validates_presence_of :content.
> when I added validateds_presence_of, rails will restrict the attribute
> not be empty, when save record. so after that, Do I still need to test
> those attributs not_valid?
> like this
> it 'should not be valid when title empty' do
>    Forum.new(empty_title_attrbiute_hash).should_not be_valid
> end
> it 'should not be valid when tag empty' do
>    Forum.new(empty_tag_attrbiute_hash).should_not be_valid
> end
> it 'should not be valid when content empty' do
>    Forum.new(empty_content_attrbiute_hash).should_not be_valid
> end

Something else to keep in mind is that in the spirit of BDD, ideally you 
are writing your specs before you write any of your code. So to write 
examples that specify attributes cannot be nil will come before the 
validates_presence_of statements in your model. Then when your tests 
pass, you know you've implemented the correct functionality. This is 
also a great sanity check when, later down the line, something gets 
accidentally changed or deleted in the model and you have a spec to 
catch it.


More information about the rspec-users mailing list