thoughtbot / shoulda

Makes tests easy on the fingers and the eyes

This URL has Read+Write access

shoulda / lib / shoulda / action_controller / macros.rb
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 1 module Shoulda # :nodoc:
33705c8b » jferris 2009-02-09 Split the Controller namesp... 2 module ActionController # :nodoc:
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 3 # = Macro test helpers for your controllers
4 #
5 # By using the macro helpers you can quickly and easily create concise and easy to read test suites.
6 #
7 # This code segment:
8 # context "on GET to :show for first record" do
9 # setup do
10 # get :show, :id => 1
11 # end
12 #
13 # should_assign_to :user
14 # should_respond_with :success
15 # should_render_template :show
16 # should_not_set_the_flash
17 #
18 # should "do something else really cool" do
19 # assert_equal 1, assigns(:user).id
20 # end
21 # end
22 #
23 # Would produce 5 tests for the +show+ action
24 module Macros
3ed10a4b » jferris 2009-01-31 Converted should_assign_to/... 25 include Matchers
26
8e13affe » jferris 2009-05-07 Deprecated should_set_the_f... 27 # Macro that creates a test asserting that the flash contains the given
28 # value. Expects a +String+ or +Regexp+.
29 #
30 # If the argument is +nil+, it will assert that the flash is not set.
31 # This behavior is deprecated.
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 32 #
33 # Example:
34 #
35 # should_set_the_flash_to "Thank you for placing this order."
36 # should_set_the_flash_to /created/i
37 def should_set_the_flash_to(val)
674163de » jferris 2009-02-03 Fixed should_set_the_flash_... 38 if val
8e13affe » jferris 2009-05-07 Deprecated should_set_the_f... 39 matcher = set_the_flash.to(val)
674163de » jferris 2009-02-03 Fixed should_set_the_flash_... 40 should matcher.description do
41 assert_accepts matcher, @controller
42 end
43 else
8e13affe » jferris 2009-05-07 Deprecated should_set_the_f... 44 warn "[DEPRECATION] should_set_the_flash_to nil is deprecated. " <<
45 "Use should_not_set_the_flash instead."
46 should_not_set_the_flash
ba7d319a » rmm5t 2008-08-31 Reorganized Controller macr... 47 end
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 48 end
ba7d319a » rmm5t 2008-08-31 Reorganized Controller macr... 49
8e13affe » jferris 2009-05-07 Deprecated should_set_the_f... 50 # Macro that creates a test asserting that the flash is empty.
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 51 def should_not_set_the_flash
8e13affe » jferris 2009-05-07 Deprecated should_set_the_f... 52 matcher = set_the_flash
53 should "not #{matcher.description}" do
54 assert_rejects matcher, @controller
55 end
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 56 end
d06e0207 » bjhess 2008-10-17 Allow symbol parameter for ... 57
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 58 # Macro that creates a test asserting that filter_parameter_logging
59 # is set for the specified keys
60 #
61 # Example:
62 #
63 # should_filter_params :password, :ssn
64 def should_filter_params(*keys)
65 keys.each do |key|
f07f56e9 » jferris 2009-02-03 Refactored should_filter_pa... 66 matcher = filter_param(key)
67 should matcher.description do
68 assert_accepts matcher, @controller
669a7619 » dancroak 2008-09-23 adding should_filter macro 69 end
70 end
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 71 end
ba7d319a » rmm5t 2008-08-31 Reorganized Controller macr... 72
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 73 # Macro that creates a test asserting that the controller assigned to
74 # each of the named instance variable(s).
75 #
76 # Options:
77 # * <tt>:class</tt> - The expected class of the instance variable being checked.
4a83ad2b » jferris 2009-04-28 Updated and corrected docum... 78 #
79 # If a block is passed, the assigned variable is expected to be equal to
80 # the return value of that block.
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 81 #
82 # Example:
83 #
84 # should_assign_to :user, :posts
85 # should_assign_to :user, :class => User
c72d8547 » jferris 2009-02-07 Added a block argument to s... 86 # should_assign_to(:user) { @user }
87 def should_assign_to(*names, &block)
9f24c1d4 » jferris 2009-05-05 Removed functionality depre... 88 klass = get_options!(names, :class)
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 89 names.each do |name|
9f24c1d4 » jferris 2009-05-05 Removed functionality depre... 90 matcher = assign_to(name).with_kind_of(klass)
91 should matcher.description do
92 if block
c72d8547 » jferris 2009-02-07 Added a block argument to s... 93 expected_value = instance_eval(&block)
94 matcher = matcher.with(expected_value)
ba7d319a » rmm5t 2008-08-31 Reorganized Controller macr... 95 end
3ed10a4b » jferris 2009-01-31 Converted should_assign_to/... 96
97 assert_accepts matcher, @controller
ba7d319a » rmm5t 2008-08-31 Reorganized Controller macr... 98 end
99 end
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 100 end
ba7d319a » rmm5t 2008-08-31 Reorganized Controller macr... 101
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 102 # Macro that creates a test asserting that the controller did not assign to
103 # any of the named instance variable(s).
104 #
105 # Example:
106 #
107 # should_not_assign_to :user, :posts
108 def should_not_assign_to(*names)
109 names.each do |name|
3ed10a4b » jferris 2009-01-31 Converted should_assign_to/... 110 matcher = assign_to(name)
111 should "not #{matcher.description}" do
112 assert_rejects matcher, @controller
ba7d319a » rmm5t 2008-08-31 Reorganized Controller macr... 113 end
114 end
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 115 end
ba7d319a » rmm5t 2008-08-31 Reorganized Controller macr... 116
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 117 # Macro that creates a test asserting that the controller responded with a 'response' status code.
118 # Example:
119 #
120 # should_respond_with :success
121 def should_respond_with(response)
122 should "respond with #{response}" do
5d0ac376 » dancroak 2009-02-04 adding respond_with matcher... 123 matcher = respond_with(response)
124 assert_accepts matcher, @controller
ba7d319a » rmm5t 2008-08-31 Reorganized Controller macr... 125 end
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 126 end
ba7d319a » rmm5t 2008-08-31 Reorganized Controller macr... 127
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 128 # Macro that creates a test asserting that the response content type was 'content_type'.
129 # Example:
130 #
131 # should_respond_with_content_type 'application/rss+xml'
132 # should_respond_with_content_type :rss
133 # should_respond_with_content_type /rss/
134 def should_respond_with_content_type(content_type)
8cef841b » jferris 2009-05-05 Added a description for res... 135 matcher = respond_with_content_type(content_type)
136 should matcher.description do
b1050a54 » dancroak 2009-02-04 made should_respond_with_co... 137 assert_accepts matcher, @controller
a7057b08 » mjankowski 2008-09-12 add a should_respond_with_c... 138 end
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 139 end
2d858c0b » mjankowski 2008-09-14 add should_render_with_layo... 140
4a83ad2b » jferris 2009-04-28 Updated and corrected docum... 141 # Macro that creates a test asserting that a value returned from the
142 # session is correct. Expects the session key as a parameter, and a block
143 # that returns the expected value.
144 #
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 145 # Example:
146 #
4a83ad2b » jferris 2009-04-28 Updated and corrected docum... 147 # should_set_session(:user_id) { @user.id }
738228fd » jferris 2009-02-09 Deprecated should_return_fr... 148 # should_set_session(:message) { "Free stuff" }
9f24c1d4 » jferris 2009-05-05 Removed functionality depre... 149 def should_set_session(key, &block)
0a2f0126 » jferris 2009-02-04 Converted should_return_fro... 150 matcher = set_session(key)
151 should matcher.description do
9f24c1d4 » jferris 2009-05-05 Removed functionality depre... 152 expected_value = instance_eval(&block)
153 matcher = matcher.to(expected_value)
3a3c576c » jferris 2009-02-07 Added a block argument to s... 154 assert_accepts matcher, @controller
c8173751 » mjankowski 2008-09-12 add a should_return_from_se... 155 end
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 156 end
c8173751 » mjankowski 2008-09-12 add a should_return_from_se... 157
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 158 # Macro that creates a test asserting that the controller rendered the given template.
159 # Example:
160 #
161 # should_render_template :new
162 def should_render_template(template)
163 should "render template #{template.inspect}" do
164 assert_template template.to_s
ba7d319a » rmm5t 2008-08-31 Reorganized Controller macr... 165 end
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 166 end
ba7d319a » rmm5t 2008-08-31 Reorganized Controller macr... 167
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 168 # Macro that creates a test asserting that the controller rendered with the given layout.
169 # Example:
170 #
171 # should_render_with_layout 'special'
172 def should_render_with_layout(expected_layout = 'application')
4a788344 » jferris 2009-02-03 Converted should_render_wit... 173 matcher = render_with_layout(expected_layout)
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 174 if expected_layout
4a788344 » jferris 2009-02-03 Converted should_render_wit... 175 should matcher.description do
176 assert_accepts matcher, @controller
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 177 end
178 else
179 should "render without layout" do
4a788344 » jferris 2009-02-03 Converted should_render_wit... 180 assert_rejects matcher, @controller
2d858c0b » mjankowski 2008-09-14 add should_render_with_layo... 181 end
182 end
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 183 end
2d858c0b » mjankowski 2008-09-14 add should_render_with_layo... 184
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 185 # Macro that creates a test asserting that the controller rendered without a layout.
186 # Same as @should_render_with_layout false@
187 def should_render_without_layout
188 should_render_with_layout nil
189 end
2d858c0b » mjankowski 2008-09-14 add should_render_with_layo... 190
4a83ad2b » jferris 2009-04-28 Updated and corrected docum... 191 # Macro that creates a test asserting that the controller returned a
192 # redirect to the given path. The passed description will be used when
193 # generating a test name. Expects a block that returns the expected path
194 # for the redirect.
195 #
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 196 # Example:
197 #
a8f0d949 » jferris 2009-02-07 Added a block option to sho... 198 # should_redirect_to("the user's profile") { user_url(@user) }
199 def should_redirect_to(description, &block)
200 should "redirect to #{description}" do
9f24c1d4 » jferris 2009-05-05 Removed functionality depre... 201 expected_url = instance_eval(&block)
202 assert_redirected_to expected_url
ba7d319a » rmm5t 2008-08-31 Reorganized Controller macr... 203 end
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 204 end
ba7d319a » rmm5t 2008-08-31 Reorganized Controller macr... 205
5c1329f6 » webmat 2009-01-05 Removed all ThoughtBot (and... 206 # Macro that creates a routing test. It tries to use the given HTTP
207 # +method+ on the given +path+, and asserts that it routes to the
208 # given +options+.
209 #
210 # If you don't specify a :controller, it will try to guess the controller
211 # based on the current test.
212 #
213 # +to_param+ is called on the +options+ given.
214 #
215 # Examples:
216 #
217 # should_route :get, "/posts", :controller => :posts, :action => :index
218 # should_route :get, "/posts/new", :action => :new
219 # should_route :post, "/posts", :action => :create
220 # should_route :get, "/posts/1", :action => :show, :id => 1
221 # should_route :edit, "/posts/1", :action => :show, :id => 1
222 # should_route :put, "/posts/1", :action => :update, :id => 1
223 # should_route :delete, "/posts/1", :action => :destroy, :id => 1
224 # should_route :get, "/users/1/posts/1",
225 # :action => :show, :id => 1, :user_id => 1
226 #
227 def should_route(method, path, options)
228 unless options[:controller]
229 options[:controller] = self.name.gsub(/ControllerTest$/, '').tableize
230 end
5996f280 » dancroak 2008-09-15 moved should_route macro in... 231
10637cf9 » jferris 2009-02-04 Converted should_route to u... 232 matcher = route(method, path).to(options)
5996f280 » dancroak 2008-09-15 moved should_route macro in... 233
10637cf9 » jferris 2009-02-04 Converted should_route to u... 234 should matcher.description do
235 assert_accepts matcher.in_context(self), self
5996f280 » dancroak 2008-09-15 moved should_route macro in... 236 end
ba7d319a » rmm5t 2008-08-31 Reorganized Controller macr... 237 end
238 end
239 end
240 end