2013-03-17 20:00:59 +00:00
|
|
|
# shoulda-matchers [![Gem Version](https://badge.fury.io/rb/shoulda-matchers.png)](http://badge.fury.io/rb/shoulda-matchers) [![Build Status](https://secure.travis-ci.org/thoughtbot/shoulda-matchers.png?branch=master)](http://travis-ci.org/thoughtbot/shoulda-matchers)
|
2012-03-09 16:57:31 +00:00
|
|
|
|
|
|
|
[Official Documentation](http://rubydoc.info/github/thoughtbot/shoulda-matchers/master/frames)
|
|
|
|
|
|
|
|
Test::Unit- and RSpec-compatible one-liners that test common Rails functionality.
|
|
|
|
These tests would otherwise be much longer, more complex, and error-prone.
|
|
|
|
|
2012-03-19 13:29:14 +00:00
|
|
|
Refer to the [shoulda-context](https://github.com/thoughtbot/shoulda-context) gem if you want to know more
|
2012-03-09 16:57:31 +00:00
|
|
|
about using shoulda with Test::Unit.
|
|
|
|
|
|
|
|
## ActiveRecord Matchers
|
|
|
|
|
|
|
|
Matchers to test associations:
|
|
|
|
|
2012-11-05 03:55:51 +00:00
|
|
|
```ruby
|
|
|
|
describe Post do
|
|
|
|
it { should belong_to(:user) }
|
|
|
|
it { should have_many(:tags).through(:taggings) }
|
|
|
|
end
|
2012-03-09 16:57:31 +00:00
|
|
|
|
2012-11-05 03:55:51 +00:00
|
|
|
describe User do
|
|
|
|
it { should have_many(:posts) }
|
|
|
|
end
|
|
|
|
```
|
2012-03-09 16:57:31 +00:00
|
|
|
|
|
|
|
## ActiveModel Matchers
|
|
|
|
|
|
|
|
Matchers to test validations and mass assignments:
|
|
|
|
|
2012-11-05 03:55:51 +00:00
|
|
|
```ruby
|
|
|
|
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) }
|
2013-08-18 17:17:19 +00:00
|
|
|
it { should have_secure_password }
|
2012-11-05 03:55:51 +00:00
|
|
|
end
|
|
|
|
```
|
2012-03-09 16:57:31 +00:00
|
|
|
|
|
|
|
## ActionController Matchers
|
|
|
|
|
|
|
|
Matchers to test common patterns:
|
|
|
|
|
2012-11-05 03:55:51 +00:00
|
|
|
```ruby
|
|
|
|
describe PostsController, "#show" do
|
|
|
|
context "for a fictional user" do
|
|
|
|
before do
|
|
|
|
get :show, :id => 1
|
2012-03-09 16:57:31 +00:00
|
|
|
end
|
|
|
|
|
2012-11-05 03:55:51 +00:00
|
|
|
it { should respond_with(:success) }
|
|
|
|
it { should render_template(:show) }
|
|
|
|
it { should_not set_the_flash }
|
2013-04-26 20:34:21 +00:00
|
|
|
it { should rescue_from(ActiveRecord::RecordNotFound).with(:render_404) }
|
2012-11-05 03:55:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2012-03-09 16:57:31 +00:00
|
|
|
## Installation
|
|
|
|
|
|
|
|
In Rails 3 and Bundler, add the following to your Gemfile:
|
|
|
|
|
2012-11-05 03:55:51 +00:00
|
|
|
```ruby
|
|
|
|
group :test do
|
|
|
|
gem "shoulda-matchers"
|
|
|
|
end
|
|
|
|
|
2013-03-20 16:38:45 +00:00
|
|
|
# `rspec-rails` needs to be in the development group so that Rails generators work.
|
2012-11-05 03:55:51 +00:00
|
|
|
group :development, :test do
|
2013-03-21 19:26:06 +00:00
|
|
|
gem "rspec-rails", "~> 2.12"
|
2012-11-05 03:55:51 +00:00
|
|
|
end
|
|
|
|
```
|
2012-06-08 16:54:08 +00:00
|
|
|
|
2012-03-09 16:57:31 +00:00
|
|
|
Shoulda will automatically include matchers into the appropriate example groups.
|
|
|
|
|
|
|
|
## Credits
|
|
|
|
|
|
|
|
Shoulda is maintained and funded by [thoughtbot](http://thoughtbot.com/community).
|
|
|
|
Thank you to all the [contributors](https://github.com/thoughtbot/shoulda-matchers/contributors).
|
|
|
|
|
|
|
|
## License
|
|
|
|
|
2013-01-06 08:56:40 +00:00
|
|
|
Shoulda is Copyright © 2006-2013 thoughtbot, inc.
|
2012-03-09 16:57:31 +00:00
|
|
|
It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
|