Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 85 lines (60 sloc) 2.608 kb
85c37c4 Gabe Berke-Williams Convert README to Markdown.
gabebw authored
1 # shoulda-matchers [![Build Status](https://secure.travis-ci.org/thoughtbot/shoulda-matchers.png?branch=master)](http://travis-ci.org/thoughtbot/shoulda-matchers)
2
3 [Official Documentation](http://rubydoc.info/github/thoughtbot/shoulda-matchers/master/frames)
4
5 Test::Unit- and RSpec-compatible one-liners that test common Rails functionality.
6 These tests would otherwise be much longer, more complex, and error-prone.
7
65d6d1c Gabe Berke-Williams Refer people to the correct projects in the README.
gabebw authored
8 Refer to the [shoulda-context](https://github.com/thoughtbot/shoulda-context) gem if you want to know more
85c37c4 Gabe Berke-Williams Convert README to Markdown.
gabebw authored
9 about using shoulda with Test::Unit.
10
11 ## ActiveRecord Matchers
12
13 Matchers to test associations:
14
15 describe Post do
16 it { should belong_to(:user) }
17 it { should have_many(:tags).through(:taggings) }
18 end
19
20 describe User do
21 it { should have_many(:posts) }
22 end
23
24 ## ActiveModel Matchers
25
26 Matchers to test validations and mass assignments:
27
28 describe Post do
29 it { should validate_uniqueness_of(:title) }
30 it { should validate_presence_of(:body).with_message(/wtf/) }
31 it { should validate_presence_of(:title) }
32 it { should validate_numericality_of(:user_id) }
02db8dc Giovanni Cappellotto Remove duplicate spec; add in_array
potomak authored
33 it { should ensure_inclusion_of(:status).in_array(['draft', 'public']) }
85c37c4 Gabe Berke-Williams Convert README to Markdown.
gabebw authored
34 end
35
36 describe User do
37 it { should_not allow_value("blah").for(:email) }
38 it { should allow_value("a@b.com").for(:email) }
39 it { should ensure_inclusion_of(:age).in_range(1..100) }
40 it { should_not allow_mass_assignment_of(:password) }
41 end
42
43 ## ActionController Matchers
44
45 Matchers to test common patterns:
46
47 describe PostsController, "#show" do
48 context "for a fictional user" do
49 before do
50 get :show, :id => 1
51 end
52
53 it { should assign_to(:user) }
54 it { should respond_with(:success) }
55 it { should render_template(:show) }
56 it { should_not set_the_flash }
57 end
58 end
59
60 ## Installation
61
62 In Rails 3 and Bundler, add the following to your Gemfile:
63
64 group :test do
65 gem "shoulda-matchers"
66 end
67
2185ea3 Gabe Berke-Williams Better installation instructions for rspec-rails. Fixes #117.
gabebw authored
68 # rspec-rails needs to be in the development group so that Rails generators
69 # work.
70 group :development, :test do
71 gem "rspec-rails"
72 end
73
85c37c4 Gabe Berke-Williams Convert README to Markdown.
gabebw authored
74 Shoulda will automatically include matchers into the appropriate example groups.
75
76 ## Credits
77
78 Shoulda is maintained and funded by [thoughtbot](http://thoughtbot.com/community).
79 Thank you to all the [contributors](https://github.com/thoughtbot/shoulda-matchers/contributors).
80
81 ## License
82
a74cee3 Adarsh Pandit Update Copyright year
adarsh authored
83 Shoulda is Copyright © 2006-2012 thoughtbot, inc.
85c37c4 Gabe Berke-Williams Convert README to Markdown.
gabebw authored
84 It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
Something went wrong with that request. Please try again.