2010-12-15 17:56:11 -05:00
|
|
|
= shoulda-matchers
|
2007-04-05 14:26:25 -04:00
|
|
|
|
2010-12-15 18:44:57 -05:00
|
|
|
{Official Documentation}[http://rubydoc.info/github/thoughtbot/shoulda-matchers/master/frames]
|
|
|
|
|
2010-12-15 17:56:11 -05:00
|
|
|
Test::Unit- and RSpec-compatible one-liners that test common Rails functionality.
|
|
|
|
These tests would otherwise be much longer, more complex, and error-prone.
|
2007-04-05 14:26:25 -04:00
|
|
|
|
2010-12-15 18:44:57 -05:00
|
|
|
Refer to the {shoulda}[https://github.com/thoughtbot/shoulda] gem if you want to know more
|
|
|
|
about using shoulda with Test::Unit.
|
|
|
|
|
2010-12-15 17:56:11 -05:00
|
|
|
=== ActiveRecord Matchers
|
2007-04-05 14:26:25 -04:00
|
|
|
|
2011-05-06 07:51:12 -04:00
|
|
|
Matchers to test associations:
|
2007-07-24 16:16:01 -04:00
|
|
|
|
2010-12-15 18:44:57 -05:00
|
|
|
describe Post do
|
|
|
|
it { should belong_to(:user) }
|
|
|
|
it { should have_many(:tags).through(:taggings) }
|
2011-05-06 07:51:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe User do
|
|
|
|
it { should have_many(:posts) }
|
|
|
|
end
|
2007-07-24 16:16:01 -04:00
|
|
|
|
2011-05-06 07:51:12 -04:00
|
|
|
=== ActiveModel Matchers
|
|
|
|
|
2011-05-09 12:17:35 -04:00
|
|
|
Matchers to test validations and mass assignments:
|
2011-05-06 07:51:12 -04:00
|
|
|
|
|
|
|
describe Post do
|
2010-12-15 18:44:57 -05:00
|
|
|
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) }
|
2007-07-24 16:16:01 -04:00
|
|
|
end
|
|
|
|
|
2010-12-15 18:44:57 -05:00
|
|
|
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) }
|
2007-07-24 16:16:01 -04:00
|
|
|
end
|
|
|
|
|
2010-12-15 18:48:29 -05:00
|
|
|
=== ActionController Matchers
|
2010-06-17 10:17:30 -04:00
|
|
|
|
2010-12-15 17:56:11 -05:00
|
|
|
Matchers to test common patterns:
|
2007-07-24 16:16:01 -04:00
|
|
|
|
2010-12-15 18:44:57 -05:00
|
|
|
describe PostsController, "#show" do
|
|
|
|
context "for a fictional user" do
|
|
|
|
before do
|
2009-05-27 13:58:39 -04:00
|
|
|
get :show, :id => 1
|
|
|
|
end
|
2007-07-24 16:16:01 -04:00
|
|
|
|
2010-12-15 18:44:57 -05:00
|
|
|
it { should assign_to(:user) }
|
|
|
|
it { should respond_with(:success) }
|
|
|
|
it { should render_template(:show) }
|
|
|
|
it { should_not set_the_flash }
|
2007-07-24 16:16:01 -04:00
|
|
|
end
|
|
|
|
end
|
2010-06-17 10:17:30 -04:00
|
|
|
|
2010-12-15 17:56:11 -05:00
|
|
|
= Installation
|
2010-06-22 16:30:54 -04:00
|
|
|
|
2010-12-15 17:56:11 -05:00
|
|
|
In Rails 3 and Bundler, add the following to your Gemfile:
|
2010-06-22 16:30:54 -04:00
|
|
|
|
|
|
|
group :test do
|
2010-12-15 17:56:11 -05:00
|
|
|
gem "rspec-rails"
|
2011-03-18 15:53:47 -04:00
|
|
|
gem "shoulda-matchers"
|
2010-06-22 16:30:54 -04:00
|
|
|
end
|
|
|
|
|
2010-12-15 17:56:11 -05:00
|
|
|
Shoulda will automatically include matchers into the appropriate example groups.
|
2010-06-22 16:30:54 -04:00
|
|
|
|
2007-07-24 16:16:01 -04:00
|
|
|
= Credits
|
|
|
|
|
2010-12-15 17:56:11 -05:00
|
|
|
Shoulda is maintained and funded by {thoughtbot}[http://thoughtbot.com/community].
|
2010-12-15 18:44:57 -05:00
|
|
|
Thank you to all the {contributors}[https://github.com/thoughtbot/shoulda-matchers/contributors].
|
2007-07-24 16:16:01 -04:00
|
|
|
|
|
|
|
= License
|
2007-04-05 14:26:25 -04:00
|
|
|
|
2010-12-15 17:56:11 -05:00
|
|
|
Shoulda is Copyright © 2006-2010 thoughtbot, inc.
|
|
|
|
It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
|