Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 8. Validations > Testing Validations with Shoulda

8.9. Testing Validations with Shoulda

Even though validations are declarative code, if you’re doing TDD then you’ll want to specify them before writing them. Luckily, Thoughtbot’s Shoulda library[4] contains a number of matchers designed to easily test validations.

[4] http://github.com/thoughtbot/shoulda

describe Post do
   it { should validate_uniqueness_of :title }
   it { should validate_presence_of :body, :message => /wtf/ }
   it { should validate_presence_of :title }
   it { should validate_numericality_of :user_id }
end

describe User do
   it { should not_allow_values_for :email, "blah", "b lah" }
   it { should allow_values_for :email, "a@b.com", "asdf@asdf.com" }
   it { should ensure_length_in_range :email, 1..100 }
   it { should ensure_value_in_range :age, 1..100 }
   it { should not_allow_mass_assignment_of :password }
 end


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint