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

Craig Demyanovich cdemyanovich at gmail.com
Wed Jul 21 07:33:22 EDT 2010


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100721/84154bf4/attachment-0001.html>


More information about the rspec-users mailing list