Simple one-liner tests for common Rails functionality
Go to file
Melissa Xie 058f39f187 Use DisallowValueMatcher for `disallows_value_of` method 2013-03-28 14:02:03 -04:00
features Require 'rspec/core' instead of 'rspec' in RSpec integration file 2013-03-21 13:57:02 -04:00
gemfiles Bump version to 1.5.4 2013-03-21 15:50:13 -04:00
lib Use DisallowValueMatcher for `disallows_value_of` method 2013-03-28 14:02:03 -04:00
spec Add a test passing mixed values to `allow_value` 2013-03-28 10:35:18 -04:00
.gitignore Unignore Gemfile.lock, per Appraisal instructions. 2012-04-10 22:55:29 -04:00
.travis.yml Allow Travis failures for Ruby 1.8.7 2013-03-18 23:55:47 -07:00
Appraisals Update 3.2.gemfile.lock 2012-12-03 08:32:44 -08:00
CONTRIBUTING.md Include step to update NEWS in contribution guidelines 2013-03-27 11:11:19 -04:00
Gemfile Uses secure RubyGems URL 2013-02-27 22:05:15 -05:00
Gemfile.lock Bump version to 1.5.4 2013-03-21 15:50:13 -04:00
MIT-LICENSE Update copyright year to 2013 2013-02-22 10:46:22 -05:00
NEWS.md Use DisallowValueMatcher for `disallows_value_of` method 2013-03-28 14:02:03 -04:00
README.md Add minimal rspec-rails version in installation instructions 2013-03-21 15:26:06 -04:00
Rakefile Allow the suite to run sequentially via Appraisal, or one at a time with BUNDLE_GEMFILE set 2013-03-04 16:10:54 -05:00
shoulda-matchers.gemspec Require 'rspec/core' instead of 'rspec' in RSpec integration file 2013-03-21 13:57:02 -04:00

README.md

shoulda-matchers Gem Version 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_uniqueness_of(:title).scoped_to(:user_id, :category_id) }
  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
  it { should permit(:title, :body).for(:create) }

  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

Independent Matchers

Matchers to test non-Rails-dependent code:

describe Human do
  it { should delegate_method(:work).to(:robot) }
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", "~> 2.12"
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-2013 thoughtbot, inc. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.