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
_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users