Skip to content

Instantly share code, notes, and snippets.

@deJaVisions
Created March 28, 2012 10:39
Show Gist options
  • Select an option

  • Save deJaVisions/2225325 to your computer and use it in GitHub Desktop.

Select an option

Save deJaVisions/2225325 to your computer and use it in GitHub Desktop.
why shoulda can be handy
# 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)
@dukedorje

Copy link
Copy Markdown

pretty cool.. declarative rather than imperative

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment