[rails] shoud I test validate_presentence_of series?

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

I like covering validation with RSpec instead of only relying on it
being
covered at a higher level. I like to check for error(s) on each
attribute to
be sure that the model is not invalid for some other reason. Here’s how
I do
it:

describe Forum, ‘being valid’ do

it “requires a name” do
forum = Forum.new(:title => nil)
forum.should_not be_valid
forum.should have_at_least(1).error_on(:title)
end

it “requires a code” do
forum = Forum.new(:tag => nil)
forum.should_not be_valid
forum.should have_at_least(1).error_on(:tag)
end

it “requires content” do
forum = Forum.new(:content => nil)
forum.should_not be_valid
forum.should have_at_least(1).error_on(:content)
end

end

Note that, in this example, you can put all of your
validates_presence_of
declarations on one line.

validates_presence_of :title, :tag, :content

Regards,
Craig

On 2010-07-21 1:41 AM, Zhenning G. wrote:

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.

Peace,
Phillip

thank you all. :slight_smile: