Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
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.
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