@@ -28,8 +28,13 @@ Initialize the `spec/` directory (where specs will reside) with:
28
28
rails generate rspec:install
29
29
```
30
30
31
- This adds ` spec/spec_helper.rb ` and ` .rspec ` files that are used for
32
- configuration. See those files for more information.
31
+ This adds the following files which are used for configuration:
32
+
33
+ - ` .rspec `
34
+ - ` spec/spec_helper.rb `
35
+ - ` spec/rails_helper.rb `
36
+
37
+ Check the comments in each file for more information.
33
38
34
39
To run your specs, use the ` rspec ` command:
35
40
@@ -53,8 +58,75 @@ be run via `bin/rspec`:
53
58
bundle binstubs rspec-core
54
59
```
55
60
61
+ ### Upgrade note
62
+
63
+ For detailed information on the RSpec 3.x upgrade process see the
64
+ [ RSpec Upgrade docs] ( https://relishapp.com/rspec/docs/upgrade ) .
65
+
66
+ There is another ` rspec-rails ` specific change to be aware of:
67
+
68
+ > The default helper files created in RSpec 3.x have changed
69
+
70
+ In prior versions, only a single ` spec_helper.rb ` file was generated. This file
71
+ has been moved to ` rails_helper.rb ` . The new ` spec_helper.rb ` is the same
72
+ standard helper generated by running ` rspec --init ` .
73
+
74
+ This change was made to accomplish two general goals:
75
+
76
+ - Keep the installation process in sync with regular RSpec changes
77
+ - Provide an out-of-the-box way to avoid loading Rails for those specs that do
78
+ not require it
79
+
80
+ ### Upgrading an Existing App
81
+
82
+ For most existing apps, one of the following upgrade paths is sufficient to
83
+ switch to the new helpers:
84
+
85
+ #### _ I need to move things over in stages_
86
+
87
+ 1 . Create a new ` rails_helper.rb ` with the following content:
88
+
89
+ ``` ruby
90
+ require ' spec_helper'
91
+ ```
92
+
93
+ 2 . As necessary, replace ` require 'spec_helper'` with ` require 'rails_helper'`
94
+ in the specs.
95
+
96
+ 3 . When ready, move any Rails specific code and setup from ` spec_helper.rb` to
97
+ ` rails_helper.rb` .
98
+
99
+ # ### _I'm ready to just switch completely_
100
+
101
+ 1 . Move the existing ` spec_helper.rb` to ` rails_helper.rb` :
102
+
103
+ ` ` ` ruby
104
+ git move spec/spec_helper.rb spec/rails_helper.rb
105
+ ` ` `
106
+
107
+ 2 . Run the installation rake task opting to not replace ` rails_helper.rb` :
108
+
109
+ ` ` ` console
110
+ $ bin/rails generate rspec:install
111
+ create .rspec
112
+ exist spec
113
+ create spec/spec_helper.rb
114
+ conflict spec/rails_helper.rb
115
+ Overwrite my_app/spec/rails_helper.rb? (enter "h"for help) [Ynaqdh] n
116
+ skip spec/rails_helper.rb
117
+ ` ` `
118
+
119
+ 3 . Move any non- Rails RSpec configurations and customizations from your
120
+ ` rails_helper.rb` to ` spec_helper.rb` .
121
+
122
+ 4 . Find / replace instances of ` require 'spec_helper'` with
123
+ ` require 'rails_helper'` in any specs which rely on Rails .
124
+
56
125
# ## Generators
57
126
127
+ ** NOTE: ** Generators run in RSpec 3 .x will use the ` rails_helper` by default.
128
+ See the above [upgrade notes](# upgrade-note) about this change.
129
+
58
130
Once installed, RSpec will generate spec files instead of Test ::Unit test files
59
131
when commands like ` rails generate model` and ` rails generate controller` are
60
132
used.
@@ -70,7 +142,7 @@ Model specs reside in the `spec/models` folder. Use model specs to describe
70
142
behavior of models (usually ActiveRecord - based) in the application. For example:
71
143
72
144
` ` ` ruby
73
- require " spec_helper "
145
+ require "rails_helper "
74
146
75
147
describe User do
76
148
it "orders by last name" do
@@ -91,7 +163,7 @@ Controller specs reside in the `spec/controllers` folder. Use controller specs
91
163
to describe behavior of Rails controllers. For example:
92
164
93
165
` ` ` ruby
94
- require " spec_helper "
166
+ require "rails_helper "
95
167
96
168
describe PostsController do
97
169
describe "GET #index" do
@@ -138,7 +210,7 @@ intent is to specify one or more request/response cycles from end to end using
138
210
a black box approach.
139
211
140
212
```ruby
141
- require ' spec_helper '
213
+ require ' rails_helper '
142
214
describe "home page" do
143
215
it "displays the user' s username after successful login" do
144
216
user = User.create!(:username => " jdoe" , :password => " secret" )
@@ -161,7 +233,7 @@ users like to use extension libraries like
161
233
[Capybara](https://github.com/jnicklas/capybara):
162
234
163
235
```ruby
164
- require ' spec_helper '
236
+ require 'rails_helper '
165
237
describe " home page" do
166
238
it " displays the user' s username after successful login" do
167
239
user = FactoryGirl.create(:user, :username => "jdoe", :password => "secret")
@@ -213,7 +285,7 @@ specs](https://www.relishapp.com/rspec/rspec-rails/v/3-0/docs/feature-specs/feat
213
285
View specs live in spec/views, and mix in ActionView::TestCase::Behavior.
214
286
215
287
```ruby
216
- require ' spec_helper '
288
+ require ' rails_helper '
217
289
describe "events/index" do
218
290
it "renders _event partial for each event" do
219
291
assign(:events, [stub_model(Event), stub_model(Event)])
@@ -318,7 +390,7 @@ expect(rendered).to xxx
318
390
Routing specs live in spec/routing.
319
391
320
392
```ruby
321
- require ' spec_helper '
393
+ require ' rails_helper '
322
394
describe "routing to profiles" do
323
395
it "routes /profile/:username to profile#show for username" do
324
396
expect(:get => "/profiles/jsmith").to route_to(
@@ -346,7 +418,7 @@ Provides a `helper` object which mixes in the helper module being spec'd, along
346
418
with ` ApplicationHelper` (if present).
347
419
348
420
` ` ` ruby
349
- require ' spec_helper '
421
+ require 'rails_helper '
350
422
describe EventsHelper do
351
423
describe "#link_to_event" do
352
424
it "displays the title, and formatted date" do
0 commit comments