Created
March 28, 2012 10:39
-
-
Save deJaVisions/2225325 to your computer and use it in GitHub Desktop.
why shoulda can be handy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # without shoulda | |
| test "invalid name gives error message" do | |
| @z.name = nil | |
| assert_presence(@z, :name) | |
| end | |
| # with shoulda | |
| class ZombieTest < ActiveSupport::TestCase | |
| should validate_presence_of(:name) | |
| should validate_presence_of(:graveyard) | |
| should ensure_length_of(:name).is_at_most(15) | |
| should have_many(:tweets) | |
| end | |
| # more | |
| should validate_uniqueness_of(:name) | |
| should ensure_length_of(:password).is_at_least(5).is_at_most(20) | |
| should validate_numericality_of(:age) | |
| should_not allow_value("blah").for(:email) | |
| should allow_value("a@b.com").for(:email) | |
| should ensure_inclusion_of(:age).in_range(1..100) | |
| should_not allow_mass_assignment_of(:password) | |
| should belong_to(:zombie) should validate_acceptance_of(:terms_of_service) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pretty cool.. declarative rather than imperative