mirror of
https://github.com/thoughtbot/shoulda-matchers.git
synced 2022-11-09 12:01:38 -05:00
Simple one-liner tests for common Rails functionality
2b68f3859b
In Rails 4, the following construct: has_many :children, conditions: { adopted: true } changes to: has_many :children, lambda { where(adopted: true) } As a result, the way we check the conditions attached to a has_many changes too: instead of accessing `reflection.options`, we have to use `reflection.scope` -- this which refers to the lambda above, so we have to evaluate it and then grab the `where` from the Relation that the lambda returns. |
||
---|---|---|
features | ||
gemfiles | ||
lib | ||
spec | ||
.gitignore | ||
.travis.yml | ||
Appraisals | ||
CONTRIBUTING.md | ||
Gemfile | ||
Gemfile.lock | ||
MIT-LICENSE | ||
NEWS.md | ||
Rakefile | ||
README.md | ||
shoulda-matchers.gemspec |
shoulda-matchers
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
context "for a fictional user" do
before do
get :show, :id => 1
end
it { should respond_with(:success) }
it { should render_template(:show) }
it { should_not set_the_flash }
it { should rescue_from(ActiveRecord::RecordNotFound).with(:render_404) }
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", "~> 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.