Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 509 lines (382 sloc) 13.86 kB
6b03bf8 @dchelimsky Take two on the travis banner (grabbed the wrong one the first time)
dchelimsky authored
1 # rspec-rails-2 [![Build Status](https://secure.travis-ci.org/rspec/rspec-rails.png?branch=master)](http://travis-ci.org/rspec/rspec-rails) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/rspec/rspec-rails)
8d0199b @dchelimsky generator fixes
dchelimsky authored
2
f30f213 @dchelimsky words
dchelimsky authored
3 rspec-2 for rails-3 with lightweight extensions to each
46cae15 @dchelimsky explain that the README aligns with the code in git HEAD
dchelimsky authored
4
0e6b185 @dchelimsky rdoc
dchelimsky authored
5 Note: Use [rspec-rails-1.3](http://github.com/dchelimsky/rspec-rails) for rails-2.
bb43457 @dchelimsky links to docs
dchelimsky authored
6
43229ac @dchelimsky words
dchelimsky authored
7 ## Install
8d0199b @dchelimsky generator fixes
dchelimsky authored
8
0e6b185 @dchelimsky rdoc
dchelimsky authored
9 ```
10 gem install rspec-rails
11 ```
c560b15 @dchelimsky words
dchelimsky authored
12
8d0199b @dchelimsky generator fixes
dchelimsky authored
13 This installs the following gems:
14
0e6b185 @dchelimsky rdoc
dchelimsky authored
15 ```
16 rspec
17 rspec-core
18 rspec-expectations
19 rspec-mocks
20 rspec-rails
21 ```
8d0199b @dchelimsky generator fixes
dchelimsky authored
22
6ab0bb6 @dchelimsky cleanup and add some rdoc
dchelimsky authored
23 ## Configure
8b32de7 @dchelimsky words
dchelimsky authored
24
f2be768 @dchelimsky refine notes from last commit
dchelimsky authored
25 Add `rspec-rails` to the `:test` and `:development` groups in the Gemfile:
8b32de7 @dchelimsky words
dchelimsky authored
26
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
27 ```ruby
28 group :test, :development do
2e2bb32 @hardbap Change it to 2.0 based on comments by @dchelimsky.
hardbap authored
29 gem "rspec-rails", "~> 2.0"
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
30 end
31 ```
8b32de7 @dchelimsky words
dchelimsky authored
32
64c16fe @dchelimsky add note about Gemfile groups to README
dchelimsky authored
33 It needs to be in the `:development` group to expose generators and rake
34 tasks without having to type `RAILS_ENV=test`.
35
62ad935 @lucascaton Adjusts in README file
lucascaton authored
36 Now you can run:
8b32de7 @dchelimsky words
dchelimsky authored
37
0e6b185 @dchelimsky rdoc
dchelimsky authored
38 ```
39 rails generate rspec:install
40 ```
8b32de7 @dchelimsky words
dchelimsky authored
41
291973b @dchelimsky remove generator doc from readme (you can see it running rails g)
dchelimsky authored
42 This adds the spec directory and some skeleton files, including
62ad935 @lucascaton Adjusts in README file
lucascaton authored
43 the "rake spec" task.
bdc6d83 @dchelimsky more about generators in README
dchelimsky authored
44
45 ### Generators
46
47 If you type `script/rails generate`, the only RSpec generator you'll actually
48 see is `rspec:install`. That's because RSpec is registered with Rails as the
49 test framework, so whenever you generate application components like models,
5d3ca6b @dchelimsky explain a bit about how generators work
dchelimsky authored
50 controllers, etc, RSpec specs are generated instead of Test::Unit tests.
8b32de7 @dchelimsky words
dchelimsky authored
51
6ab0bb6 @dchelimsky cleanup and add some rdoc
dchelimsky authored
52 Please note that the generators are there to help you get started, but they are
53 no substitute for writing your own examples, and they are only guaranteed to
80febb5 @dchelimsky Consolidate rdoc for example groups to README
dchelimsky authored
54 work out of the box for the default scenario (`ActiveRecord` & `Webrat`).
bdc6d83 @dchelimsky more about generators in README
dchelimsky authored
55
9bf3e32 @dchelimsky rdoc
dchelimsky authored
56 ### Webrat and Capybara
57
58 You can choose between webrat or capybara for simulating a browser, automating
59 a browser, or setting expectations using the matchers they supply. Just add
60 your preference to the Gemfile:
61
62 ```ruby
63 gem "webrat"
64 gem "capybara"
65 ```
66
273e124 @dchelimsky words
dchelimsky authored
67 ## Living on edge
68
99b7c5c @dchelimsky words
dchelimsky authored
69 Bundler makes it a snap to use the latest code for any gem your app depends on. For
70 rspec-rails, you'll need to point bundler to the git repositories for `rspec-rails`
71 and the other rspec related gems it depends on:
273e124 @dchelimsky words
dchelimsky authored
72
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
73 ```ruby
74 gem "rspec-rails", :git => "git://github.com/rspec/rspec-rails.git"
75 gem "rspec", :git => "git://github.com/rspec/rspec.git"
76 gem "rspec-core", :git => "git://github.com/rspec/rspec-core.git"
77 gem "rspec-expectations", :git => "git://github.com/rspec/rspec-expectations.git"
78 gem "rspec-mocks", :git => "git://github.com/rspec/rspec-mocks.git"
79 ```
273e124 @dchelimsky words
dchelimsky authored
80
99b7c5c @dchelimsky words
dchelimsky authored
81 Run `bundle install` and you'll have whatever is in git right now. Any time you
82 want to update to a newer head, just run `bundle update`.
83
84 Keep in mind that each of these codebases is under active development, which
85 means that its entirely possible that you'll pull from these repos and they won't
86 play nice together. If playing nice is important to you, stick to the published
87 gems.
273e124 @dchelimsky words
dchelimsky authored
88
ab72884 @dchelimsky more words
dchelimsky authored
89 ## Backwards compatibility
90
91 This is a complete rewrite of the rspec-rails extension designed to work with
92 rails-3.x and rspec-2.x. It will not work with older versions of either rspec
93 or rails. Many of the APIs from rspec-rails-1 have been carried forward,
94 however, so upgrading an app from rspec-1/rails-2, while not pain-free, should
95 not send you to the doctor with a migraine.
8d0199b @dchelimsky generator fixes
dchelimsky authored
96
43229ac @dchelimsky words
dchelimsky authored
97 ## Known issues
8d0199b @dchelimsky generator fixes
dchelimsky authored
98
9387d57 @dchelimsky words
dchelimsky authored
99 See http://github.com/rspec/rspec-rails/issues
100
101 # Request Specs
102
6a87a0b @dchelimsky beef up README section on Request Specs
dchelimsky authored
103 Request specs live in spec/requests, and mix in behavior
104 [ActionDispatch::Integration::Runner](http://api.rubyonrails.org/classes/ActionDispatch/Integration/Runner.html),
105 which is the basis for [Rails' integration
106 tests](http://guides.rubyonrails.org/testing.html#integration-testing). The
107 intent is to specify one or more request/response cycles from end to end using
108 a black box approach.
f30f213 @dchelimsky words
dchelimsky authored
109
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
110 ```ruby
72ceb3c Include require 'spec_helper' in README code examples, fixes #581
Jordan Goldstein authored
111 require 'spec_helper'
6a87a0b @dchelimsky beef up README section on Request Specs
dchelimsky authored
112 describe "home page" do
6207edc @mlineen Update README.md
mlineen authored
113 it "displays the user's username after successful login" do
6a87a0b @dchelimsky beef up README section on Request Specs
dchelimsky authored
114 user = User.create!(:username => "jdoe", :password => "secret")
115 get "/login"
116 assert_select "form.login" do
117 assert_select "input[name=?]", "username"
118 assert_select "input[name=?]", "password"
119 assert_select "input[type=?]", "submit"
f30f213 @dchelimsky words
dchelimsky authored
120 end
6a87a0b @dchelimsky beef up README section on Request Specs
dchelimsky authored
121
122 post "/login", :username => "jdoe", :password => "secret"
123 assert_select ".header .username", :text => "jdoe"
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
124 end
125 end
126 ```
f30f213 @dchelimsky words
dchelimsky authored
127
6a87a0b @dchelimsky beef up README section on Request Specs
dchelimsky authored
128 This example uses only standard Rails and RSpec API's, but many RSpec/Rails
129 users like to use extension libraries like FactoryGirl and Capybara:
130
131 ```ruby
72ceb3c Include require 'spec_helper' in README code examples, fixes #581
Jordan Goldstein authored
132 require 'spec_helper'
6a87a0b @dchelimsky beef up README section on Request Specs
dchelimsky authored
133 describe "home page" do
6207edc @mlineen Update README.md
mlineen authored
134 it "displays the user's username after successful login" do
13bf2a4 @bricker Update Factory Girl examples to replace deprecated syntax
bricker authored
135 user = FactoryGirl.create(:user, :username => "jdoe", :password => "secret")
6a87a0b @dchelimsky beef up README section on Request Specs
dchelimsky authored
136 visit "/login"
137 fill_in "Username", :with => "jdoe"
138 fill_in "Password", :with => "secret"
03a2f6f @reidreid46 Fix method name type 'click_buton'
reidreid46 authored
139 click_button "Log in"
6a87a0b @dchelimsky beef up README section on Request Specs
dchelimsky authored
140
9324260 @alindeman README uses expect() syntax
alindeman authored
141 expect(page).to have_selector(".header .username", :text => "jdoe")
6a87a0b @dchelimsky beef up README section on Request Specs
dchelimsky authored
142 end
143 end
144 ```
145
146 FactoryGirl decouples this example from changes to validation requirements,
147 which can be encoded into the underlying factory definition without requiring
148 changes to this example.
149
150 Among other benefits, Capybara binds the form post to the generated HTML, which
151 means we don't need to specify them separately.
152
153 There are several other Ruby libs that implement the factory pattern or provide
154 a DSL for request specs (a.k.a. acceptance or integration specs), but
155 FactoryGirl and Capybara seem to be the most widely used. Whether you choose
156 these or other libs, we strongly recommend using something for each of these
157 roles.
8d0199b @dchelimsky generator fixes
dchelimsky authored
158
43229ac @dchelimsky words
dchelimsky authored
159 # Controller Specs
0dcfce7 @dchelimsky words
dchelimsky authored
160
3d173af @dchelimsky words
dchelimsky authored
161 Controller specs live in spec/controllers, and mix in
80febb5 @dchelimsky Consolidate rdoc for example groups to README
dchelimsky authored
162 ActionController::TestCase::Behavior, which is the basis for Rails' functional
163 tests.
0dcfce7 @dchelimsky words
dchelimsky authored
164
0e6b185 @dchelimsky rdoc
dchelimsky authored
165 ## with fixtures
80febb5 @dchelimsky Consolidate rdoc for example groups to README
dchelimsky authored
166
167 ```ruby
72ceb3c Include require 'spec_helper' in README code examples, fixes #581
Jordan Goldstein authored
168 require 'spec_helper'
80febb5 @dchelimsky Consolidate rdoc for example groups to README
dchelimsky authored
169 describe WidgetsController do
170 describe "GET index" do
171 fixtures :widgets
172
173 it "assigns all widgets to @widgets" do
174 get :index
9324260 @alindeman README uses expect() syntax
alindeman authored
175 expect(assigns(:widgets)).to eq(Widget.all)
80febb5 @dchelimsky Consolidate rdoc for example groups to README
dchelimsky authored
176 end
177 end
178 end
179 ```
180
0e6b185 @dchelimsky rdoc
dchelimsky authored
181 ## with a factory
80febb5 @dchelimsky Consolidate rdoc for example groups to README
dchelimsky authored
182
183 ```ruby
72ceb3c Include require 'spec_helper' in README code examples, fixes #581
Jordan Goldstein authored
184 require 'spec_helper'
80febb5 @dchelimsky Consolidate rdoc for example groups to README
dchelimsky authored
185 describe WidgetsController do
186 describe "GET index" do
187 it "assigns all widgets to @widgets" do
13bf2a4 @bricker Update Factory Girl examples to replace deprecated syntax
bricker authored
188 widget = FactoryGirl.create(:widget)
80febb5 @dchelimsky Consolidate rdoc for example groups to README
dchelimsky authored
189 get :index
9324260 @alindeman README uses expect() syntax
alindeman authored
190 expect(assigns(:widgets)).to eq([widget])
80febb5 @dchelimsky Consolidate rdoc for example groups to README
dchelimsky authored
191 end
192 end
193 end
194 ```
195
196 ## with stubs
197
198 ```ruby
72ceb3c Include require 'spec_helper' in README code examples, fixes #581
Jordan Goldstein authored
199 require 'spec_helper'
80febb5 @dchelimsky Consolidate rdoc for example groups to README
dchelimsky authored
200 describe WidgetsController do
201 describe "GET index" do
202 it "assigns all widgets to @widgets" do
203 widget = stub_model(Widget)
204 Widget.stub(:all) { [widget] }
205 get :index
9324260 @alindeman README uses expect() syntax
alindeman authored
206 expect(assigns(:widgets)).to eq([widget])
80febb5 @dchelimsky Consolidate rdoc for example groups to README
dchelimsky authored
207 end
208 end
209 end
210 ```
211
0e6b185 @dchelimsky rdoc
dchelimsky authored
212 ## matchers
80febb5 @dchelimsky Consolidate rdoc for example groups to README
dchelimsky authored
213
214 In addition to the stock matchers from rspec-expectations, controller
215 specs add these matchers, which delegate to rails' assertions:
216
217 ```ruby
9324260 @alindeman README uses expect() syntax
alindeman authored
218 expect(response).to render_template(*args)
80febb5 @dchelimsky Consolidate rdoc for example groups to README
dchelimsky authored
219 # => delegates to assert_template(*args)
220
9324260 @alindeman README uses expect() syntax
alindeman authored
221 expect(response).to redirect_to(destination)
80febb5 @dchelimsky Consolidate rdoc for example groups to README
dchelimsky authored
222 # => delegates to assert_redirected_to(destination)
223 ```
224
0e6b185 @dchelimsky rdoc
dchelimsky authored
225 ## isolation from views
80febb5 @dchelimsky Consolidate rdoc for example groups to README
dchelimsky authored
226
227 RSpec's preferred approach to spec'ing controller behaviour is to isolate
228 the controller from its collaborators. By default, therefore, controller
229 example groups do not render the views in your app. Due to the way Rails
230 searches for view templates, the template still needs to exist, but it
231 won't actually be loaded.
232
233 NOTE that this is different from rspec-rails-1 with rails-2, which did not
234 require the presence of the file at all. Due to changes in rails-3, this
235 was no longer feasible in rspec-rails-2.
38df36b @dchelimsky more words
dchelimsky authored
236
435c181 @dchelimsky add upgrade notes about methods that changed to README
dchelimsky authored
237 ## `render_views`
9bf3e32 @dchelimsky rdoc
dchelimsky authored
238
80febb5 @dchelimsky Consolidate rdoc for example groups to README
dchelimsky authored
239 If you prefer a more integrated approach, similar to that of Rails'
240 functional tests, you can tell controller groups to render the views in the
241 app with the `render_views` declaration:
43229ac @dchelimsky words
dchelimsky authored
242
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
243 ```ruby
72ceb3c Include require 'spec_helper' in README code examples, fixes #581
Jordan Goldstein authored
244 require 'spec_helper'
80febb5 @dchelimsky Consolidate rdoc for example groups to README
dchelimsky authored
245 describe WidgetsController do
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
246 render_views
247 # ...
248 ```
43229ac @dchelimsky words
dchelimsky authored
249
9bf3e32 @dchelimsky rdoc
dchelimsky authored
250 ### Upgrade note
435c181 @dchelimsky add upgrade notes about methods that changed to README
dchelimsky authored
251
252 `render_views` replaces `integrate_views` from rspec-rails-1.3
253
254 ## `assigns`
255
256 Use `assigns(key)` to express expectations about instance variables that a controller
257 assigns to the view in the course of an action:
258
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
259 ```ruby
260 get :index
9324260 @alindeman README uses expect() syntax
alindeman authored
261 expect(assigns(:widgets)).to eq(expected_value)
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
262 ```
435c181 @dchelimsky add upgrade notes about methods that changed to README
dchelimsky authored
263
9387d57 @dchelimsky words
dchelimsky authored
264 # View specs
265
266 View specs live in spec/views, and mix in ActionView::TestCase::Behavior.
267
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
268 ```ruby
72ceb3c Include require 'spec_helper' in README code examples, fixes #581
Jordan Goldstein authored
269 require 'spec_helper'
ff96af6 @dchelimsky remove rendering engine and mime type from calls to describe and rend…
dchelimsky authored
270 describe "events/index" do
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
271 it "renders _event partial for each event" do
272 assign(:events, [stub_model(Event), stub_model(Event)])
273 render
9324260 @alindeman README uses expect() syntax
alindeman authored
274 expect(view).to render_template(:partial => "_event", :count => 2)
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
275 end
276 end
277
ff96af6 @dchelimsky remove rendering engine and mime type from calls to describe and rend…
dchelimsky authored
278 describe "events/show" do
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
279 it "displays the event location" do
280 assign(:event, stub_model(Event,
281 :location => "Chicago"
282 ))
283 render
9324260 @alindeman README uses expect() syntax
alindeman authored
284 expect(rendered).to contain("Chicago")
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
285 end
286 end
287 ```
cbf623a @wincent doc: note that ActionView::TestCase auto-propagates ivars
wincent authored
288
732a922 @dchelimsky Infer controller.request.path_params["controller"] from the file being
dchelimsky authored
289 View specs infer the controller name and path from the path to the view
290 template. e.g. if the template is "events/index.html.erb" then:
291
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
292 ```ruby
293 controller.controller_path == "events"
294 controller.request.path_parameters[:controller] == "events"
295 ```
732a922 @dchelimsky Infer controller.request.path_params["controller"] from the file being
dchelimsky authored
296
297 This means that most of the time you don't need to set these values. When
298 spec'ing a partial that is included across different controllers, you _may_
299 need to override these values before rendering the view.
300
30d998c @rgarner Explicit template/layout render example
rgarner authored
301 To provide a layout for the render, you'll need to specify _both_ the template
302 and the layout explicitly. For example:
303
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
304 ```ruby
305 render :template => "events/show", :layout => "layouts/application"
306 ```
30d998c @rgarner Explicit template/layout render example
rgarner authored
307
435c181 @dchelimsky add upgrade notes about methods that changed to README
dchelimsky authored
308 ## `assign(key, val)`
309
310 Use this to assign values to instance variables in the view:
311
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
312 ```ruby
313 assign(:widget, stub_model(Widget))
314 render
315 ```
cbf623a @wincent doc: note that ActionView::TestCase auto-propagates ivars
wincent authored
316
435c181 @dchelimsky add upgrade notes about methods that changed to README
dchelimsky authored
317 The code above assigns `stub_model(Widget)` to the `@widget` variable in the view, and then
318 renders the view.
319
cbf623a @wincent doc: note that ActionView::TestCase auto-propagates ivars
wincent authored
320 Note that because view specs mix in `ActionView::TestCase` behavior, any
321 instance variables you set will be transparently propagated into your views
322 (similar to how instance variables you set in controller actions are made
323 available in views). For example:
324
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
325 ```ruby
326 @widget = stub_model(Widget)
327 render # @widget is available inside the view
328 ```
cbf623a @wincent doc: note that ActionView::TestCase auto-propagates ivars
wincent authored
329
330 RSpec doesn't officially support this pattern, which only works as a
331 side-effect of the inclusion of `ActionView::TestCase`. Be aware that it may be
332 made unavailable in the future.
333
9bf3e32 @dchelimsky rdoc
dchelimsky authored
334 ### Upgrade note
435c181 @dchelimsky add upgrade notes about methods that changed to README
dchelimsky authored
335
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
336 ```ruby
337 # rspec-rails-1.x
338 assigns[key] = value
339
340 # rspec-rails-2.x
341 assign(key, value)
342 ```
435c181 @dchelimsky add upgrade notes about methods that changed to README
dchelimsky authored
343
344 ## `rendered`
345
346 This represents the rendered view.
347
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
348 ```ruby
349 render
9324260 @alindeman README uses expect() syntax
alindeman authored
350 expect(rendered).to match /Some text expected to appear on the page/
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
351 ```
99ba346 @dchelimsky Revert "Use Github Flavored Markdown"
dchelimsky authored
352
9bf3e32 @dchelimsky rdoc
dchelimsky authored
353 ### Upgrade note
435c181 @dchelimsky add upgrade notes about methods that changed to README
dchelimsky authored
354
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
355 ```ruby
356 # rspec-rails-1.x
357 render
358 response.should xxx
359
360 # rspec-rails-2.x
361 render
362 rendered.should xxx
9324260 @alindeman README uses expect() syntax
alindeman authored
363
364 # rspec-rails-2.x with expect syntax
365 render
366 expect(rendered).to xxx
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
367 ```
9387d57 @dchelimsky words
dchelimsky authored
368
9bf3e32 @dchelimsky rdoc
dchelimsky authored
369 # Model specs
370
371 Model specs live in spec/models.
372
373 ```ruby
72ceb3c Include require 'spec_helper' in README code examples, fixes #581
Jordan Goldstein authored
374 require 'spec_helper'
70dfecd @moonmaster9000 Fix typo in README (Articles -> Article)
moonmaster9000 authored
375 describe Article do
9bf3e32 @dchelimsky rdoc
dchelimsky authored
376 describe ".recent" do
377 it "includes articles published less than one week ago" do
378 article = Article.create!(:published_at => Date.today - 1.week + 1.second)
9324260 @alindeman README uses expect() syntax
alindeman authored
379 expect(Article.recent).to eq([article])
9bf3e32 @dchelimsky rdoc
dchelimsky authored
380 end
381
382 it "excludes articles published at midnight one week ago" do
383 article = Article.create!(:published_at => Date.today - 1.week)
9324260 @alindeman README uses expect() syntax
alindeman authored
384 expect(Article.recent).to be_empty
9bf3e32 @dchelimsky rdoc
dchelimsky authored
385 end
386
387 it "excludes articles published more than one week ago" do
388 article = Article.create!(:published_at => Date.today - 1.week - 1.second)
9324260 @alindeman README uses expect() syntax
alindeman authored
389 expect(Article.recent).to be_empty
9bf3e32 @dchelimsky rdoc
dchelimsky authored
390 end
391 end
392 end
393 ```
394
f504614 @dchelimsky words
dchelimsky authored
395 # Routing specs
396
397 Routing specs live in spec/routing.
398
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
399 ```ruby
72ceb3c Include require 'spec_helper' in README code examples, fixes #581
Jordan Goldstein authored
400 require 'spec_helper'
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
401 describe "routing to profiles" do
402 it "routes /profile/:username to profile#show for username" do
9324260 @alindeman README uses expect() syntax
alindeman authored
403 expect(:get => "/profiles/jsmith").to route_to(
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
404 :controller => "profiles",
405 :action => "show",
406 :username => "jsmith"
407 )
408 end
409
410 it "does not expose a list of profiles" do
9324260 @alindeman README uses expect() syntax
alindeman authored
411 expect(:get => "/profiles").not_to be_routable
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
412 end
413 end
414 ```
f504614 @dchelimsky words
dchelimsky authored
415
9bf3e32 @dchelimsky rdoc
dchelimsky authored
416 ### Upgrade note
cd4dcbd @dchelimsky note about route_for being removed
dchelimsky authored
417
418 `route_for` from rspec-rails-1.x is gone. Use `route_to` and `be_routable` instead.
419
9387d57 @dchelimsky words
dchelimsky authored
420 # Helper specs
421
422 Helper specs live in spec/helpers, and mix in ActionView::TestCase::Behavior.
423
80febb5 @dchelimsky Consolidate rdoc for example groups to README
dchelimsky authored
424 Provides a `helper` object which mixes in the helper module being spec'd, along
425 with `ApplicationHelper` (if present).
426
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
427 ```ruby
72ceb3c Include require 'spec_helper' in README code examples, fixes #581
Jordan Goldstein authored
428 require 'spec_helper'
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
429 describe EventsHelper do
430 describe "#link_to_event" do
431 it "displays the title, and formatted date" do
432 event = Event.new("Ruby Kaigi", Date.new(2010, 8, 27))
433 # helper is an instance of ActionView::Base configured with the
434 # EventsHelper and all of Rails' built-in helpers
9324260 @alindeman README uses expect() syntax
alindeman authored
435 expect(helper.link_to_event).to match /Ruby Kaigi, 27 Aug, 2010/
9387d57 @dchelimsky words
dchelimsky authored
436 end
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
437 end
438 end
439 ```
f30f213 @dchelimsky words
dchelimsky authored
440
441 # Matchers
442
443 rspec-rails exposes domain-specific matchers to each of the example group types. Most
444 of them simply delegate to Rails' assertions.
445
446 ## `be_a_new`
447 * Available in all specs.
448 * Primarily intended for controller specs
449
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
450 ```ruby
9324260 @alindeman README uses expect() syntax
alindeman authored
451 expect(object).to be_a_new(Widget)
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
452 ```
453
f30f213 @dchelimsky words
dchelimsky authored
454
455 Passes if the object is a `Widget` and returns true for `new_record?`
456
457 ## `render_template`
458 * Delegates to Rails' assert_template.
459 * Available in request, controller, and view specs.
460
461 In request and controller specs, apply to the response object:
462
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
463 ```ruby
9324260 @alindeman README uses expect() syntax
alindeman authored
464 expect(response).to render_template("new")
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
465 ```
f30f213 @dchelimsky words
dchelimsky authored
466
467 In view specs, apply to the view object:
468
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
469 ```ruby
9324260 @alindeman README uses expect() syntax
alindeman authored
470 expect(view).to render_template(:partial => "_form", :locals => { :widget => widget } )
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
471 ```
f30f213 @dchelimsky words
dchelimsky authored
472
473 ## `redirect_to`
474 * Delegates to assert_redirect
475 * Available in request and controller specs.
476
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
477 ```ruby
9324260 @alindeman README uses expect() syntax
alindeman authored
478 expect(response).to redirect_to(widgets_path)
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
479 ```
f30f213 @dchelimsky words
dchelimsky authored
480
481 ## `route_to`
482
483 * Delegates to Rails' assert_routing.
484 * Available in routing and controller specs.
485
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
486 ```ruby
9324260 @alindeman README uses expect() syntax
alindeman authored
487 expect(:get => "/widgets").to route_to(:controller => "widgets", :action => "index")
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
488 ```
f30f213 @dchelimsky words
dchelimsky authored
489
490 ## `be_routable`
491
492 Passes if the path is recognized by Rails' routing. This is primarily intended
9324260 @alindeman README uses expect() syntax
alindeman authored
493 to be used with `not_to` to specify routes that should not be routable.
f30f213 @dchelimsky words
dchelimsky authored
494
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
495 ```ruby
9324260 @alindeman README uses expect() syntax
alindeman authored
496 expect(:get => "/widgets/1/edit").not_to be_routable
1bec8ff @dchelimsky use github-flavored-markdown in the readme
dchelimsky authored
497 ```
6e90564 @dchelimsky words
dchelimsky authored
498
9bf3e32 @dchelimsky rdoc
dchelimsky authored
499 # Contribute
6e90564 @dchelimsky words
dchelimsky authored
500
501 See [http://github.com/rspec/rspec-dev](http://github.com/rspec/rspec-dev)
502
9bf3e32 @dchelimsky rdoc
dchelimsky authored
503 # Also see
6e90564 @dchelimsky words
dchelimsky authored
504
505 * [http://github.com/rspec/rspec](http://github.com/rspec/rspec)
506 * [http://github.com/rspec/rspec-core](http://github.com/rspec/rspec-core)
507 * [http://github.com/rspec/rspec-expectations](http://github.com/rspec/rspec-expectations)
508 * [http://github.com/rspec/rspec-mocks](http://github.com/rspec/rspec-mocks)
Something went wrong with that request. Please try again.