Simple one-liner tests for common Rails functionality
Go to file
Jason Draper b45105980a Don't memoize the message finder so that we can make sure there is an instance when we need it 2012-10-15 11:31:45 -04:00
features Mix RSpec with Test::Unit 2012-07-11 12:17:04 +02:00
gemfiles Updated description of AllowMassAssignmentMatcher. 2012-10-08 09:33:16 -04:00
lib Don't memoize the message finder so that we can make sure there is an instance when we need it 2012-10-15 11:31:45 -04:00
spec Don't memoize the message finder so that we can make sure there is an instance when we need it 2012-10-15 11:31:45 -04:00
.gitignore Unignore Gemfile.lock, per Appraisal instructions. 2012-04-10 22:55:29 -04:00
.travis.yml Test against Rails 3.2 in Travis. 2012-10-01 13:03:15 -04:00
Appraisals Update appraisals to latest Rails versions. 2012-10-01 15:36:04 -04:00
CONTRIBUTING.md Update contribution guidelines. 2012-03-18 20:39:15 -04:00
Gemfile get jruby passing 2012-04-13 10:11:42 -04:00
Gemfile.lock Bump version to 1.4.0 2012-10-05 13:25:13 -04:00
MIT-LICENSE converted to the MIT license 2007-11-26 15:18:29 +00:00
NEWS.md Bump version to 1.4.0 2012-10-05 13:25:13 -04:00
README.md Update Copyright year 2012-09-14 10:51:55 -07:00
Rakefile Clean up Rakefile. 2012-04-10 22:53:11 -04:00
shoulda-matchers.gemspec Whoops, don't use Bundler 1.0 2012-09-21 15:19:41 -04:00

README.md

shoulda-matchers Build Status

Official Documentation

Test::Unit- and RSpec-compatible one-liners that test common Rails functionality. These tests would otherwise be much longer, more complex, and error-prone.

Refer to the shoulda-context gem if you want to know more about using shoulda with Test::Unit.

ActiveRecord Matchers

Matchers to test associations:

describe Post do
  it { should belong_to(:user) }
  it { should have_many(:tags).through(:taggings) }
end

describe User do
  it { should have_many(:posts) }
end

ActiveModel Matchers

Matchers to test validations and mass assignments:

describe Post do
  it { should validate_uniqueness_of(:title) }
  it { should validate_presence_of(:body).with_message(/wtf/) }
  it { should validate_presence_of(:title) }
  it { should validate_numericality_of(:user_id) }
  it { should ensure_inclusion_of(:status).in_array(['draft', 'public']) }
end

describe User do
  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) }
end

ActionController Matchers

Matchers to test common patterns:

describe PostsController, "#show" do
  context "for a fictional user" do
    before do
      get :show, :id => 1
    end

    it { should assign_to(:user) }
    it { should respond_with(:success) }
    it { should render_template(:show) }
    it { should_not set_the_flash }
  end
end

Installation

In Rails 3 and Bundler, add the following to your Gemfile:

group :test do
  gem "shoulda-matchers"
end

# rspec-rails needs to be in the development group so that Rails generators
# work.
group :development, :test do
  gem "rspec-rails"
end

Shoulda will automatically include matchers into the appropriate example groups.

Credits

Shoulda is maintained and funded by thoughtbot. Thank you to all the contributors.

License

Shoulda is Copyright © 2006-2012 thoughtbot, inc. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.