Skip to content

Commit 2347e7c

Browse files
committed
Merge pull request #1060 from rspec/fix-readme-for-rails-helper
Properly reference `rails_helper` in README.
2 parents 2fd0780 + 1ff006e commit 2347e7c

File tree

2 files changed

+153
-10
lines changed

2 files changed

+153
-10
lines changed

README.md

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ Initialize the `spec/` directory (where specs will reside) with:
2828
rails generate rspec:install
2929
```
3030

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.
3338

3439
To run your specs, use the `rspec` command:
3540

@@ -53,8 +58,75 @@ be run via `bin/rspec`:
5358
bundle binstubs rspec-core
5459
```
5560

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+
56125
### Generators
57126

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+
58130
Once installed, RSpec will generate spec files instead of Test::Unit test files
59131
when commands like `rails generate model` and `rails generate controller` are
60132
used.
@@ -70,7 +142,7 @@ Model specs reside in the `spec/models` folder. Use model specs to describe
70142
behavior of models (usually ActiveRecord-based) in the application. For example:
71143

72144
```ruby
73-
require "spec_helper"
145+
require "rails_helper"
74146
75147
describe User do
76148
it "orders by last name" do
@@ -91,7 +163,7 @@ Controller specs reside in the `spec/controllers` folder. Use controller specs
91163
to describe behavior of Rails controllers. For example:
92164

93165
```ruby
94-
require "spec_helper"
166+
require "rails_helper"
95167
96168
describe PostsController do
97169
describe "GET #index" do
@@ -138,7 +210,7 @@ intent is to specify one or more request/response cycles from end to end using
138210
a black box approach.
139211
140212
```ruby
141-
require 'spec_helper'
213+
require 'rails_helper'
142214
describe "home page" do
143215
it "displays the user's username after successful login" do
144216
user = User.create!(:username => "jdoe", :password => "secret")
@@ -161,7 +233,7 @@ users like to use extension libraries like
161233
[Capybara](https://github.com/jnicklas/capybara):
162234
163235
```ruby
164-
require 'spec_helper'
236+
require 'rails_helper'
165237
describe "home page" do
166238
it "displays the user's username after successful login" do
167239
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
213285
View specs live in spec/views, and mix in ActionView::TestCase::Behavior.
214286
215287
```ruby
216-
require 'spec_helper'
288+
require 'rails_helper'
217289
describe "events/index" do
218290
it "renders _event partial for each event" do
219291
assign(:events, [stub_model(Event), stub_model(Event)])
@@ -318,7 +390,7 @@ expect(rendered).to xxx
318390
Routing specs live in spec/routing.
319391
320392
```ruby
321-
require 'spec_helper'
393+
require 'rails_helper'
322394
describe "routing to profiles" do
323395
it "routes /profile/:username to profile#show for username" do
324396
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
346418
with `ApplicationHelper` (if present).
347419

348420
```ruby
349-
require 'spec_helper'
421+
require 'rails_helper'
350422
describe EventsHelper do
351423
describe "#link_to_event" do
352424
it "displays the title, and formatted date" do

features/Upgrade.md

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,75 @@
1-
# Upgrading from rspec-rails-1.x to rspec-rails-2.
1+
# Upgrading from rspec-rails-2.x to rspec-rails-3
2+
3+
For detailed information on the RSpec 3.x upgrade process see the [RSpec Upgrade
4+
docs](https://relishapp.com/rspec/docs/upgrade).
5+
6+
There is another `rspec-rails` specific change to be aware of:
7+
8+
```text
9+
The default helper files created in RSpec 3.x have changed
10+
```
11+
12+
In prior versions, only a single `spec_helper.rb` file was generated. This file
13+
has been moved to `rails_helper.rb`. The new `spec_helper.rb` is the same
14+
standard helper generated by running `rspec --init`.
15+
16+
This change was made to accomplish two general goals:
17+
18+
- Keep the installation process in sync with regular RSpec changes
19+
20+
- Provide an out-of-the-box way to avoid loading Rails for those specs that do
21+
not require it
22+
23+
## Upgrading an Existing App
24+
25+
For most existing apps, one of the following upgrade paths is sufficient to
26+
switch to the new helpers:
27+
28+
### I need to move things over in stages
29+
30+
1. Create a new `rails_helper.rb` with the following content:
31+
32+
```ruby
33+
require 'spec_helper'
34+
```
35+
36+
2. As necessary, replace `require 'spec_helper'` with `require 'rails_helper'`
37+
in the specs.
38+
39+
3. When ready, move any Rails specific code and setup from `spec_helper.rb` to
40+
`rails_helper.rb`.
41+
42+
### I'm ready to just switch completely
43+
44+
1. Move the existing `spec_helper.rb` to `rails_helper.rb`:
45+
46+
```ruby
47+
git move spec/spec_helper.rb spec/rails_helper.rb
48+
```
49+
50+
2. Run the installation rake task opting to not replace `rails_helper.rb`:
51+
52+
```console
53+
$ bin/rails generate rspec:install
54+
create .rspec
55+
exist spec
56+
create spec/spec_helper.rb
57+
conflict spec/rails_helper.rb
58+
Overwrite my_app/spec/rails_helper.rb? (enter "h"for help) [Ynaqdh] n
59+
skip spec/rails_helper.rb
60+
```
61+
62+
3. Move any non-Rails RSpec configurations and customizations from your
63+
`rails_helper.rb` to `spec_helper.rb`.
64+
65+
4. Find/replace instances of `require 'spec_helper'` with
66+
`require 'rails_helper'` in any specs which rely on Rails.
67+
68+
## Generators
69+
70+
Generators run in RSpec 3.x will use the `rails_helper` by default.
71+
72+
# Upgrading from rspec-rails-1.x to rspec-rails-2
273

374
This is a work in progress. Please submit errata, missing steps, or patches to
475
the [rspec-rails issue tracker](https://github.com/rspec/rspec-rails/issues).

0 commit comments

Comments
 (0)