Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 155 lines (108 sloc) 4.607 kb
e9eba10 documentation and fixes to :through test
tsaleh authored
1 = Shoulda - Making tests easy on the fingers and eyes
9bf2ba7 documentation
tsaleh authored
2
f3e56ed Dan Croak more updates to README: replacing Macros references to Matchers where ap...
croaky authored
3 Shoulda makes it easy to write elegant, understandable, and maintainable tests. Shoulda consists of matchers, test helpers, and assertions. It's fully compatible with your existing tests in Test::Unit or RSpec, and requires no retooling to use.
9bf2ba7 documentation
tsaleh authored
4
f3e56ed Dan Croak more updates to README: replacing Macros references to Matchers where ap...
croaky authored
5 Matchers:: 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 Helpers:: #context and #should give you RSpec like test blocks in Test::Unit.
0ea1fc7 Tons of documentation
tsaleh authored
8 In addition, you get nested contexts and a much more readable syntax.
f3e56ed Dan Croak more updates to README: replacing Macros references to Matchers where ap...
croaky authored
9 Assertions:: Many common Rails testing idioms have been distilled into a set of useful assertions.
9bf2ba7 documentation
tsaleh authored
10
0ea1fc7 Tons of documentation
tsaleh authored
11 = Usage
12
f3e56ed Dan Croak more updates to README: replacing Macros references to Matchers where ap...
croaky authored
13 === ActiveRecord Tests (Shoulda::ActiveRecord::Matchers)
526b123 Ryan McGeary Added shoulda/assertions and moved generic assertions into it
rmm5t authored
14
f3e56ed Dan Croak more updates to README: replacing Macros references to Matchers where ap...
croaky authored
15 Test your ActiveRecord associations and validations with these powerful matchers:
0ea1fc7 Tons of documentation
tsaleh authored
16
17 class PostTest < Test::Unit::TestCase
53450b1 Mike Burns Update the README for syntax changes; may need more work
mike-burns authored
18 should belong_to(:user)
19 should have_many(:tags).through(:taggings)
0ea1fc7 Tons of documentation
tsaleh authored
20
53450b1 Mike Burns Update the README for syntax changes; may need more work
mike-burns authored
21 should validate_uniqueness_of(:title)
22 should validate_presence_of(:body).with_message(/wtf/)
23 should validate_presence_of(:title)
24 should validate_numericality_of(:user_id)
0ea1fc7 Tons of documentation
tsaleh authored
25 end
26
27 class UserTest < Test::Unit::TestCase
53450b1 Mike Burns Update the README for syntax changes; may need more work
mike-burns authored
28 should have_many(:posts)
29
30 should_not allow_value("blah").for(:email)
31 should_not allow_value("b lah").for(:email)
32 should allow_value("a@b.com").for(:email)
33 should allow_value("asdf@asdf.com").for(:email)
34 should ensure_inclusion_of(:email).in_range(1..100)
35 should ensure_inclusion_of(:age).in_range(1..100)
36 should_not allow_mass_assignment_of(:password)
0ea1fc7 Tons of documentation
tsaleh authored
37 end
38
39 Makes TDD so much easier.
40
f3e56ed Dan Croak more updates to README: replacing Macros references to Matchers where ap...
croaky authored
41 === Controller Tests (Shoulda::Controller::Matchers)
42
43 Matchers to test the most common controller patterns...
0ea1fc7 Tons of documentation
tsaleh authored
44
fdb4b51 Sean Schofield Added clarification to README
schof authored
45 class PostsControllerTest < ActionController::TestCase
46 context "on GET to :show for first record" do
47 setup do
48 get :show, :id => 1
49 end
0ea1fc7 Tons of documentation
tsaleh authored
50
53450b1 Mike Burns Update the README for syntax changes; may need more work
mike-burns authored
51 should assign_to(:user)
52 should respond_with(:success)
53 should render_template(:show)
54 should_not set_the_flash
0ea1fc7 Tons of documentation
tsaleh authored
55
fdb4b51 Sean Schofield Added clarification to README
schof authored
56 should "do something else really cool" do
57 assert_equal 1, assigns(:user).id
58 end
0ea1fc7 Tons of documentation
tsaleh authored
59 end
60 end
f3e56ed Dan Croak more updates to README: replacing Macros references to Matchers where ap...
croaky authored
61
62 === Context Helpers (Shoulda::Context)
63
64 Stop killing your fingers with all of those underscores... Name your tests with plain sentences!
65
66 class UserTest < Test::Unit::TestCase
67 context "A User instance" do
68 setup do
69 @user = User.find(:first)
70 end
71
72 should "return its full name" do
73 assert_equal 'John Doe', @user.full_name
74 end
75
76 context "with a profile" do
77 setup do
78 @user.profile = Profile.find(:first)
79 end
80
81 should "return true when sent #has_profile?" do
82 assert @user.has_profile?
83 end
84 end
85 end
86 end
87
88 Produces the following test methods:
89
90 "test: A User instance should return its full name."
91 "test: A User instance with a profile should return true when sent #has_profile?."
92
93 So readable!
94
5c1329f Mathieu Martin Removed all ThoughtBot (and Thoughtbot) namespaces
webmat authored
95 === Helpful Assertions (Shoulda::Assertions)
0ea1fc7 Tons of documentation
tsaleh authored
96
97 More to come here, but have fun with what's there.
98
99 assert_same_elements([:a, :b, :c], [:c, :a, :b])
100 assert_contains(['a', '1'], /\d/)
101 assert_contains(['a', '1'], 'a')
102
4d32c50 Joe Ferris Added RSpec installation instructions
jferris authored
103 = Rails Installation (Test::Unit)
bab3b04 Ryan McGeary Added a section on Rails Installation to the README
rmm5t authored
104
f2068b9 readme updates for installation and credits
Chad Pytel authored
105 Specify the gem dependency in your config/environment.rb file:
bab3b04 Ryan McGeary Added a section on Rails Installation to the README
rmm5t authored
106
107 Rails::Initializer.run do |config|
385907e Dan Croak source for gem is not gems.github.com
croaky authored
108 config.gem "shoulda", :lib => "shoulda"
bab3b04 Ryan McGeary Added a section on Rails Installation to the README
rmm5t authored
109 end
110
111 Then:
112
113 $ rake gems:install
114 $ rake gems:unpack
115
4d32c50 Joe Ferris Added RSpec installation instructions
jferris authored
116 = Rails Installation (RSpec)
117
18eafea Joe Ferris Clarified in the README that the RSpec instructions are only for RSpec
jferris authored
118 If you're using Shoulda with RSpec, we recommend that you add config.gem lines
119 for RSpec and Shoulda in your config/environment/test.rb file, but do not ask
120 Rails to load the RSpec and Shoulda libraries:
4d32c50 Joe Ferris Added RSpec installation instructions
jferris authored
121
385907e Dan Croak source for gem is not gems.github.com
croaky authored
122 config.gem 'rspec', :lib => false
4d32c50 Joe Ferris Added RSpec installation instructions
jferris authored
123 config.gem 'rspec-rails', :lib => false
385907e Dan Croak source for gem is not gems.github.com
croaky authored
124 config.gem 'shoulda', :lib => false
4d32c50 Joe Ferris Added RSpec installation instructions
jferris authored
125
126 Then require shoulda from your spec/spec_helper.rb file, before Spec::Runner is
127 configured:
128
129 # requires for RSpec
130 require 'shoulda'
131 Spec::Runner.configure do |config|
132 # ...
133
134 You should not need to require anything besides the top-level shoulda library.
135
5db8e5b Joshua Clayton Update readme to reflect Rails 3 installation instructions
joshuaclayton authored
136 = Rails 3 Installation (RSpec)
137
138 With Rails 3 and Bundler, requiring Shoulda is as easy as adding it to your Gemfile:
139
140 group :test do
141 gem "shoulda"
142 gem "rspec-rails", "2.0.0.beta.12"
143 end
144
16e1d90 Joe Ferris Updated README and version
jferris authored
145 Shoulda will automatically include matchers into the appropriate example
146 groups.
5db8e5b Joshua Clayton Update readme to reflect Rails 3 installation instructions
joshuaclayton authored
147
0ea1fc7 Tons of documentation
tsaleh authored
148 = Credits
149
a839bf5 Matt Jankowski remove reference to evil twin organization
mjankowski authored
150 Shoulda is maintained and funded by {thoughtbot}[http://thoughtbot.com/community]
0ea1fc7 Tons of documentation
tsaleh authored
151
152 = License
9bf2ba7 documentation
tsaleh authored
153
385907e Dan Croak source for gem is not gems.github.com
croaky authored
154 Shoulda is Copyright © 2006-2010 Tammer Saleh, Thoughtbot. 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.