Shoulda matcher is a library that enables to write better and more understandable tests for Rails application. It is Test::Unit- and RSpec-compatible one-liners that test common Rails functionality.
Here few very easier example of shoulda-matchers for Unit Testing.
Test model fields:
it {should have_db_column(:login)} it {should have_db_column(:salary).of_type(:decimal).with_options(:precision => 10, :scale => 2) } it { should_not have_db_column(:admin).of_type(:boolean) }
Test db indexes:
it { should have_db_index(:age) } it { should have_db_index([:commentable_type, :commentable_id]) } it { should have_db_index(:ssn).unique(true) }
Test validations:
it { should validate_uniqueness_of(:title) } it { should validate_presence_of(:body).with_message(/Enter the message/) } it { should validate_presence_of(:title) } it { should validate_numericality_of(:user_id) } it { should validate_uniqueness_of(:title) } it { should_not allow_value("blah").for(:email) } it { should allow_value("a@b.com").for(:email) } it { should ensure_inclusion_of(:age).in_range(1..100) } it { should_not allow_mass_assignment_of(:password) }
Test associations:
it { should belong_to(:parent) } it { should have_one(:car) it { should have_many(:friends) } it { should have_many(:enemies).through(:friends) }
Further reading:
Shoulda-Matcher home page – https://github.com/thoughtbot/shoulda-matchers/
Shoulda-context home page – https://github.com/thoughtbot/shoulda-context/
rspec_shoulda cheat sheet – http://cheat.errtheblog.com/s/rspec_shoulda/