rspec-rails 2.99.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +6 -14
- checksums.yaml.gz.sig +1 -0
- data/.yardopts +1 -1
- data/Capybara.md +1 -3
- data/Changelog.md +148 -11
- data/License.txt +1 -0
- data/README.md +202 -105
- data/lib/generators/rspec/controller/controller_generator.rb +1 -0
- data/lib/generators/rspec/controller/templates/controller_spec.rb +5 -5
- data/lib/generators/rspec/controller/templates/view_spec.rb +2 -2
- data/lib/generators/rspec/feature/feature_generator.rb +16 -0
- data/lib/generators/rspec/feature/templates/feature_spec.rb +5 -0
- data/lib/generators/rspec/helper/helper_generator.rb +1 -0
- data/lib/generators/rspec/helper/templates/helper_spec.rb +2 -2
- data/lib/generators/rspec/install/install_generator.rb +44 -5
- data/lib/generators/rspec/install/templates/spec/{spec_helper.rb.tt → rails_helper.rb} +19 -28
- data/lib/generators/rspec/integration/integration_generator.rb +1 -12
- data/lib/generators/rspec/integration/templates/request_spec.rb +3 -8
- data/lib/generators/rspec/job/job_generator.rb +12 -0
- data/lib/generators/rspec/job/templates/job_spec.rb.erb +7 -0
- data/lib/generators/rspec/mailer/mailer_generator.rb +1 -0
- data/lib/generators/rspec/mailer/templates/mailer_spec.rb +6 -6
- data/lib/generators/rspec/model/model_generator.rb +19 -5
- data/lib/generators/rspec/model/templates/fixtures.yml +1 -1
- data/lib/generators/rspec/model/templates/model_spec.rb +2 -2
- data/lib/generators/rspec/observer/observer_generator.rb +1 -0
- data/lib/generators/rspec/observer/templates/observer_spec.rb +2 -2
- data/lib/generators/rspec/scaffold/scaffold_generator.rb +108 -135
- data/lib/generators/rspec/scaffold/templates/controller_spec.rb +34 -39
- data/lib/generators/rspec/scaffold/templates/edit_spec.rb +5 -13
- data/lib/generators/rspec/scaffold/templates/index_spec.rb +3 -10
- data/lib/generators/rspec/scaffold/templates/new_spec.rb +6 -14
- data/lib/generators/rspec/scaffold/templates/routing_spec.rb +9 -9
- data/lib/generators/rspec/scaffold/templates/show_spec.rb +4 -11
- data/lib/generators/rspec/view/templates/view_spec.rb +2 -2
- data/lib/generators/rspec/view/view_generator.rb +1 -0
- data/lib/generators/rspec.rb +16 -6
- data/lib/rspec/rails/adapters.rb +33 -35
- data/lib/rspec/rails/configuration.rb +97 -0
- data/lib/rspec/rails/example/controller_example_group.rb +173 -159
- data/lib/rspec/rails/example/feature_example_group.rb +25 -20
- data/lib/rspec/rails/example/helper_example_group.rb +27 -26
- data/lib/rspec/rails/example/mailer_example_group.rb +29 -14
- data/lib/rspec/rails/example/model_example_group.rb +7 -7
- data/lib/rspec/rails/example/rails_example_group.rb +1 -0
- data/lib/rspec/rails/example/request_example_group.rb +19 -17
- data/lib/rspec/rails/example/routing_example_group.rb +40 -39
- data/lib/rspec/rails/example/view_example_group.rb +140 -137
- data/lib/rspec/rails/example.rb +0 -33
- data/lib/rspec/rails/extensions/active_record/proxy.rb +0 -1
- data/lib/rspec/rails/extensions.rb +0 -1
- data/lib/rspec/rails/feature_check.rb +35 -0
- data/lib/rspec/rails/fixture_support.rb +5 -12
- data/lib/rspec/rails/matchers/be_a_new.rb +67 -62
- data/lib/rspec/rails/matchers/be_new_record.rb +23 -21
- data/lib/rspec/rails/matchers/be_valid.rb +41 -33
- data/lib/rspec/rails/matchers/have_http_status.rb +361 -0
- data/lib/rspec/rails/matchers/have_rendered.rb +35 -31
- data/lib/rspec/rails/matchers/redirect_to.rb +30 -29
- data/lib/rspec/rails/matchers/relation_match_array.rb +1 -1
- data/lib/rspec/rails/matchers/routing_matchers.rb +104 -94
- data/lib/rspec/rails/matchers.rb +7 -4
- data/lib/rspec/rails/tasks/rspec.rake +1 -1
- data/lib/rspec/rails/vendor/capybara.rb +2 -33
- data/lib/rspec/rails/version.rb +3 -1
- data/lib/rspec/rails/view_assigns.rb +18 -18
- data/lib/rspec/rails/view_rendering.rb +20 -45
- data/lib/rspec/rails.rb +2 -13
- data/lib/rspec-rails.rb +4 -1
- data.tar.gz.sig +0 -0
- metadata +80 -99
- metadata.gz.sig +3 -0
- data/lib/autotest/rails_rspec2.rb +0 -91
- data/lib/generators/rspec/install/templates/.rspec +0 -1
- data/lib/rspec/rails/extensions/active_record/base.rb +0 -58
- data/lib/rspec/rails/infer_type_configuration.rb +0 -26
- data/lib/rspec/rails/matchers/have_extension.rb +0 -36
- data/lib/rspec/rails/mocks.rb +0 -284
- data/lib/rspec/rails/module_inclusion.rb +0 -19
- data/lib/rspec/rails/vendor/webrat.rb +0 -33
data/README.md
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
# rspec-rails [](http://travis-ci.org/rspec/rspec-rails) [](https://codeclimate.com/github/rspec/rspec-rails)
|
|
2
|
-
|
|
3
2
|
**rspec-rails** is a testing framework for Rails 3.x and 4.x.
|
|
4
3
|
|
|
5
4
|
Use **[rspec-rails 1.x](http://github.com/dchelimsky/rspec-rails)** for Rails
|
|
@@ -12,7 +11,7 @@ Add `rspec-rails` to **both** the `:development` and `:test` groups in the
|
|
|
12
11
|
|
|
13
12
|
```ruby
|
|
14
13
|
group :development, :test do
|
|
15
|
-
gem 'rspec-rails', '~>
|
|
14
|
+
gem 'rspec-rails', '~> 3.0.0'
|
|
16
15
|
end
|
|
17
16
|
```
|
|
18
17
|
|
|
@@ -28,11 +27,27 @@ Initialize the `spec/` directory (where specs will reside) with:
|
|
|
28
27
|
rails generate rspec:install
|
|
29
28
|
```
|
|
30
29
|
|
|
31
|
-
|
|
30
|
+
This adds the following files which are used for configuration:
|
|
31
|
+
|
|
32
|
+
- `.rspec`
|
|
33
|
+
- `spec/spec_helper.rb`
|
|
34
|
+
- `spec/rails_helper.rb`
|
|
35
|
+
|
|
36
|
+
Check the comments in each file for more information.
|
|
37
|
+
|
|
38
|
+
Use the `rspec` command to run your specs:
|
|
32
39
|
|
|
33
40
|
```
|
|
34
41
|
bundle exec rspec
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
By default the above will run all `_spec.rb` files in the `spec` directory. For
|
|
45
|
+
more details about this see the [RSpec spec file
|
|
46
|
+
docs](https://www.relishapp.com/rspec/rspec-core/docs/spec-files).
|
|
35
47
|
|
|
48
|
+
To run only a subset of these specs use the following command:
|
|
49
|
+
|
|
50
|
+
```
|
|
36
51
|
# Run only model specs
|
|
37
52
|
bundle exec rspec spec/models
|
|
38
53
|
|
|
@@ -50,26 +65,52 @@ be run via `bin/rspec`:
|
|
|
50
65
|
bundle binstubs rspec-core
|
|
51
66
|
```
|
|
52
67
|
|
|
68
|
+
### Upgrade Note
|
|
69
|
+
|
|
70
|
+
For detailed information on the general RSpec 3.x upgrade process see the
|
|
71
|
+
[RSpec Upgrade docs](https://relishapp.com/rspec/docs/upgrade).
|
|
72
|
+
|
|
73
|
+
There are three particular `rspec-rails` specific changes to be aware of:
|
|
74
|
+
|
|
75
|
+
1. [The default helper files created in RSpec 3.x have changed](https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#default-helper-files)
|
|
76
|
+
2. [File-type inference disabled by default](https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#file-type-inference-disabled)
|
|
77
|
+
3. [Rails 4.x `ActiveRecord::Migration` pending migration checks](https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#pending-migration-checks)
|
|
78
|
+
4. Extraction of `stub_model` and `mock_model` to
|
|
79
|
+
[`rspec-activemodel-mocks`](https://github.com/rspec/rspec-activemodel-mocks)
|
|
80
|
+
|
|
81
|
+
Please see the [RSpec Rails Upgrade
|
|
82
|
+
docs](https://www.relishapp.com/rspec/rspec-rails/docs/upgrade) for full
|
|
83
|
+
details.
|
|
84
|
+
|
|
85
|
+
**NOTE:** Generators run in RSpec 3.x will now require `rails_helper` instead
|
|
86
|
+
of `spec_helper`.
|
|
87
|
+
|
|
53
88
|
### Generators
|
|
54
89
|
|
|
55
90
|
Once installed, RSpec will generate spec files instead of Test::Unit test files
|
|
56
91
|
when commands like `rails generate model` and `rails generate controller` are
|
|
57
92
|
used.
|
|
58
93
|
|
|
59
|
-
You may also invoke
|
|
94
|
+
You may also invoke RSpec generators independently. For instance,
|
|
60
95
|
running `rails generate rspec:model` will generate a model spec. For more
|
|
61
96
|
information, see [list of all
|
|
62
97
|
generators](https://www.relishapp.com/rspec/rspec-rails/docs/generators).
|
|
63
98
|
|
|
64
99
|
## Model Specs
|
|
65
100
|
|
|
66
|
-
|
|
67
|
-
|
|
101
|
+
Use model specs to describe behavior of models (usually ActiveRecord-based) in
|
|
102
|
+
the application.
|
|
103
|
+
|
|
104
|
+
Model specs default to residing in the `spec/models` folder. Tagging any
|
|
105
|
+
context with the metadata `:type => :model` treats its examples as model
|
|
106
|
+
specs.
|
|
107
|
+
|
|
108
|
+
For example:
|
|
68
109
|
|
|
69
110
|
```ruby
|
|
70
|
-
require "
|
|
111
|
+
require "rails_helper"
|
|
71
112
|
|
|
72
|
-
describe User do
|
|
113
|
+
RSpec.describe User, :type => :model do
|
|
73
114
|
it "orders by last name" do
|
|
74
115
|
lindeman = User.create!(first_name: "Andy", last_name: "Lindeman")
|
|
75
116
|
chelimsky = User.create!(first_name: "David", last_name: "Chelimsky")
|
|
@@ -84,18 +125,23 @@ specs](https://www.relishapp.com/rspec/rspec-rails/docs/model-specs).
|
|
|
84
125
|
|
|
85
126
|
## Controller Specs
|
|
86
127
|
|
|
87
|
-
|
|
88
|
-
|
|
128
|
+
Use controller specs to describe behavior of Rails controllers.
|
|
129
|
+
|
|
130
|
+
Controller specs default to residing in the `spec/controllers` folder. Tagging
|
|
131
|
+
any context with the metadata `:type => :controller` treats its examples as
|
|
132
|
+
controller specs.
|
|
133
|
+
|
|
134
|
+
For example:
|
|
89
135
|
|
|
90
136
|
```ruby
|
|
91
|
-
require "
|
|
137
|
+
require "rails_helper"
|
|
92
138
|
|
|
93
|
-
describe PostsController do
|
|
139
|
+
RSpec.describe PostsController, :type => :controller do
|
|
94
140
|
describe "GET #index" do
|
|
95
141
|
it "responds successfully with an HTTP 200 status code" do
|
|
96
142
|
get :index
|
|
97
143
|
expect(response).to be_success
|
|
98
|
-
expect(response
|
|
144
|
+
expect(response).to have_http_status(200)
|
|
99
145
|
end
|
|
100
146
|
|
|
101
147
|
it "renders the index template" do
|
|
@@ -117,25 +163,31 @@ For more information, see [cucumber scenarios for controller
|
|
|
117
163
|
specs](https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs).
|
|
118
164
|
|
|
119
165
|
**Note:** To encourage more isolated testing, views are not rendered by default
|
|
120
|
-
in controller specs. If you
|
|
121
|
-
view
|
|
166
|
+
in controller specs. If you are verifying discrete view logic, use a [view
|
|
167
|
+
spec](#view-specs). If you are verifying the behaviour of a controller and view
|
|
168
|
+
together, consider a [request spec](#request-specs). You can use
|
|
122
169
|
[render\_views](https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/render-views)
|
|
123
|
-
|
|
124
|
-
|
|
170
|
+
if you must verify the rendered view contents within a controller spec, but
|
|
171
|
+
this is not recommended.
|
|
172
|
+
|
|
173
|
+
## Request Specs
|
|
125
174
|
|
|
126
|
-
|
|
175
|
+
Use request specs to specify one or more request/response cycles from end to
|
|
176
|
+
end using a black box approach.
|
|
127
177
|
|
|
128
|
-
Request specs
|
|
129
|
-
spec/integration
|
|
178
|
+
Request specs default to residing in the `spec/requests`, `spec/api`, and
|
|
179
|
+
`spec/integration` directories. Tagging any context with the metadata `:type =>
|
|
180
|
+
:request` treats its examples as request specs.
|
|
181
|
+
|
|
182
|
+
Request specs mix in behavior from
|
|
130
183
|
[ActionDispatch::Integration::Runner](http://api.rubyonrails.org/classes/ActionDispatch/Integration/Runner.html),
|
|
131
184
|
which is the basis for [Rails' integration
|
|
132
|
-
tests](http://guides.rubyonrails.org/testing.html#integration-testing).
|
|
133
|
-
intent is to specify one or more request/response cycles from end to end using
|
|
134
|
-
a black box approach.
|
|
185
|
+
tests](http://guides.rubyonrails.org/testing.html#integration-testing).
|
|
135
186
|
|
|
136
187
|
```ruby
|
|
137
|
-
require '
|
|
138
|
-
|
|
188
|
+
require 'rails_helper'
|
|
189
|
+
|
|
190
|
+
RSpec.describe "home page", :type => :request do
|
|
139
191
|
it "displays the user's username after successful login" do
|
|
140
192
|
user = User.create!(:username => "jdoe", :password => "secret")
|
|
141
193
|
get "/login"
|
|
@@ -151,14 +203,15 @@ describe "home page" do
|
|
|
151
203
|
end
|
|
152
204
|
```
|
|
153
205
|
|
|
154
|
-
|
|
155
|
-
users like to use extension libraries like
|
|
206
|
+
The above example uses only standard Rails and RSpec API's, but many
|
|
207
|
+
RSpec/Rails users like to use extension libraries like
|
|
156
208
|
[FactoryGirl](https://github.com/thoughtbot/factory_girl) and
|
|
157
209
|
[Capybara](https://github.com/jnicklas/capybara):
|
|
158
210
|
|
|
159
211
|
```ruby
|
|
160
|
-
require '
|
|
161
|
-
|
|
212
|
+
require 'rails_helper'
|
|
213
|
+
|
|
214
|
+
RSpec.describe "home page", :type => :request do
|
|
162
215
|
it "displays the user's username after successful login" do
|
|
163
216
|
user = FactoryGirl.create(:user, :username => "jdoe", :password => "secret")
|
|
164
217
|
visit "/login"
|
|
@@ -176,7 +229,7 @@ which can be encoded into the underlying factory definition without requiring
|
|
|
176
229
|
changes to this example.
|
|
177
230
|
|
|
178
231
|
Among other benefits, Capybara binds the form post to the generated HTML, which
|
|
179
|
-
means we don't need to specify them separately.
|
|
232
|
+
means we don't need to specify them separately. Note that Capybara's DSL as
|
|
180
233
|
shown is, by default, only available in specs in the spec/features directory.
|
|
181
234
|
For more information, see the [Capybara integration
|
|
182
235
|
docs](http://rubydoc.info/gems/rspec-rails/file/Capybara.md).
|
|
@@ -187,25 +240,47 @@ FactoryGirl and Capybara seem to be the most widely used. Whether you choose
|
|
|
187
240
|
these or other libs, we strongly recommend using something for each of these
|
|
188
241
|
roles.
|
|
189
242
|
|
|
190
|
-
|
|
243
|
+
## Feature Specs
|
|
244
|
+
|
|
245
|
+
Feature specs test your application from the outside by simulating a browser.
|
|
246
|
+
[`capybara`](https://github.com/jnicklas/capybara) is used to manage the
|
|
247
|
+
simulated browser.
|
|
248
|
+
|
|
249
|
+
Feature specs default to residing in the `spec/features` folder. Tagging any
|
|
250
|
+
context with the metadata `:type => :feature` treats its examples as feature
|
|
251
|
+
specs.
|
|
252
|
+
|
|
253
|
+
Feature specs mix in functionality from the capybara gem, thus they require
|
|
254
|
+
`capybara` to use. To use feature specs, add `capybara` to the `Gemfile`:
|
|
255
|
+
|
|
256
|
+
```ruby
|
|
257
|
+
gem "capybara"
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
For more information, see the [cucumber scenarios for feature
|
|
261
|
+
specs](https://www.relishapp.com/rspec/rspec-rails/v/3-0/docs/feature-specs/feature-spec).
|
|
262
|
+
|
|
263
|
+
## View specs
|
|
191
264
|
|
|
192
|
-
View specs
|
|
265
|
+
View specs default to residing in the `spec/views` folder. Tagging any context
|
|
266
|
+
with the metadata `:type => :view` treats its examples as view specs.
|
|
267
|
+
|
|
268
|
+
View specs mix in `ActionView::TestCase::Behavior`.
|
|
193
269
|
|
|
194
270
|
```ruby
|
|
195
|
-
require '
|
|
196
|
-
|
|
271
|
+
require 'rails_helper'
|
|
272
|
+
|
|
273
|
+
RSpec.describe "events/index", :type => :view do
|
|
197
274
|
it "renders _event partial for each event" do
|
|
198
|
-
assign(:events, [
|
|
275
|
+
assign(:events, [double(Event), double(Event)])
|
|
199
276
|
render
|
|
200
277
|
expect(view).to render_template(:partial => "_event", :count => 2)
|
|
201
278
|
end
|
|
202
279
|
end
|
|
203
280
|
|
|
204
|
-
describe "events/show" do
|
|
281
|
+
RSpec.describe "events/show", :type => :view do
|
|
205
282
|
it "displays the event location" do
|
|
206
|
-
assign(:event,
|
|
207
|
-
:location => "Chicago"
|
|
208
|
-
))
|
|
283
|
+
assign(:event, Event.new(:location => "Chicago"))
|
|
209
284
|
render
|
|
210
285
|
expect(rendered).to include("Chicago")
|
|
211
286
|
end
|
|
@@ -213,7 +288,7 @@ end
|
|
|
213
288
|
```
|
|
214
289
|
|
|
215
290
|
View specs infer the controller name and path from the path to the view
|
|
216
|
-
template. e.g. if the template is
|
|
291
|
+
template. e.g. if the template is `events/index.html.erb` then:
|
|
217
292
|
|
|
218
293
|
```ruby
|
|
219
294
|
controller.controller_path == "events"
|
|
@@ -225,23 +300,23 @@ spec'ing a partial that is included across different controllers, you _may_
|
|
|
225
300
|
need to override these values before rendering the view.
|
|
226
301
|
|
|
227
302
|
To provide a layout for the render, you'll need to specify _both_ the template
|
|
228
|
-
and the layout explicitly.
|
|
303
|
+
and the layout explicitly. For example:
|
|
229
304
|
|
|
230
305
|
```ruby
|
|
231
306
|
render :template => "events/show", :layout => "layouts/application"
|
|
232
307
|
```
|
|
233
308
|
|
|
234
|
-
|
|
309
|
+
### `assign(key, val)`
|
|
235
310
|
|
|
236
311
|
Use this to assign values to instance variables in the view:
|
|
237
312
|
|
|
238
313
|
```ruby
|
|
239
|
-
assign(:widget,
|
|
314
|
+
assign(:widget, Widget.new)
|
|
240
315
|
render
|
|
241
316
|
```
|
|
242
317
|
|
|
243
|
-
The code above assigns `
|
|
244
|
-
renders the view.
|
|
318
|
+
The code above assigns `Widget.new` to the `@widget` variable in the view, and
|
|
319
|
+
then renders the view.
|
|
245
320
|
|
|
246
321
|
Note that because view specs mix in `ActionView::TestCase` behavior, any
|
|
247
322
|
instance variables you set will be transparently propagated into your views
|
|
@@ -249,7 +324,7 @@ instance variables you set will be transparently propagated into your views
|
|
|
249
324
|
available in views). For example:
|
|
250
325
|
|
|
251
326
|
```ruby
|
|
252
|
-
@widget =
|
|
327
|
+
@widget = Widget.new
|
|
253
328
|
render # @widget is available inside the view
|
|
254
329
|
```
|
|
255
330
|
|
|
@@ -257,17 +332,17 @@ RSpec doesn't officially support this pattern, which only works as a
|
|
|
257
332
|
side-effect of the inclusion of `ActionView::TestCase`. Be aware that it may be
|
|
258
333
|
made unavailable in the future.
|
|
259
334
|
|
|
260
|
-
|
|
335
|
+
#### Upgrade note
|
|
261
336
|
|
|
262
337
|
```ruby
|
|
263
338
|
# rspec-rails-1.x
|
|
264
339
|
assigns[key] = value
|
|
265
340
|
|
|
266
|
-
# rspec-rails-2.x
|
|
341
|
+
# rspec-rails-2.x+
|
|
267
342
|
assign(key, value)
|
|
268
343
|
```
|
|
269
344
|
|
|
270
|
-
|
|
345
|
+
### `rendered`
|
|
271
346
|
|
|
272
347
|
This represents the rendered view.
|
|
273
348
|
|
|
@@ -276,29 +351,32 @@ render
|
|
|
276
351
|
expect(rendered).to match /Some text expected to appear on the page/
|
|
277
352
|
```
|
|
278
353
|
|
|
279
|
-
|
|
354
|
+
#### Upgrade note
|
|
280
355
|
|
|
281
356
|
```ruby
|
|
282
357
|
# rspec-rails-1.x
|
|
283
358
|
render
|
|
284
359
|
response.should xxx
|
|
285
360
|
|
|
286
|
-
# rspec-rails-2.x
|
|
361
|
+
# rspec-rails-2.x+
|
|
287
362
|
render
|
|
288
363
|
rendered.should xxx
|
|
289
364
|
|
|
290
|
-
# rspec-rails-2.x with expect syntax
|
|
365
|
+
# rspec-rails-2.x+ with expect syntax
|
|
291
366
|
render
|
|
292
367
|
expect(rendered).to xxx
|
|
293
368
|
```
|
|
294
369
|
|
|
295
|
-
|
|
370
|
+
## Routing specs
|
|
296
371
|
|
|
297
|
-
Routing specs
|
|
372
|
+
Routing specs default to residing in the `spec/routing` folder. Tagging any
|
|
373
|
+
context with the metadata `:type => :routing` treats its examples as routing
|
|
374
|
+
specs.
|
|
298
375
|
|
|
299
376
|
```ruby
|
|
300
|
-
require '
|
|
301
|
-
|
|
377
|
+
require 'rails_helper'
|
|
378
|
+
|
|
379
|
+
RSpec.describe "routing to profiles", :type => :routing do
|
|
302
380
|
it "routes /profile/:username to profile#show for username" do
|
|
303
381
|
expect(:get => "/profiles/jsmith").to route_to(
|
|
304
382
|
:controller => "profiles",
|
|
@@ -315,18 +393,23 @@ end
|
|
|
315
393
|
|
|
316
394
|
### Upgrade note
|
|
317
395
|
|
|
318
|
-
`route_for` from rspec-rails-1.x is gone. Use `route_to` and `be_routable`
|
|
396
|
+
`route_for` from rspec-rails-1.x is gone. Use `route_to` and `be_routable`
|
|
397
|
+
instead.
|
|
319
398
|
|
|
320
|
-
|
|
399
|
+
## Helper specs
|
|
321
400
|
|
|
322
|
-
Helper specs
|
|
401
|
+
Helper specs default to residing in the `spec/helpers` folder. Tagging any
|
|
402
|
+
context with the metadata `:type => :helper` treats its examples as helper
|
|
403
|
+
specs.
|
|
323
404
|
|
|
324
|
-
|
|
325
|
-
|
|
405
|
+
Helper specs mix in ActionView::TestCase::Behavior. A `helper` object is
|
|
406
|
+
provided which mixes in the helper module being spec'd, along with
|
|
407
|
+
`ApplicationHelper` (if present).
|
|
326
408
|
|
|
327
409
|
```ruby
|
|
328
|
-
require '
|
|
329
|
-
|
|
410
|
+
require 'rails_helper'
|
|
411
|
+
|
|
412
|
+
RSpec.describe EventsHelper, :type => :helper do
|
|
330
413
|
describe "#link_to_event" do
|
|
331
414
|
it "displays the title, and formatted date" do
|
|
332
415
|
event = Event.new("Ruby Kaigi", Date.new(2010, 8, 27))
|
|
@@ -338,73 +421,102 @@ describe EventsHelper do
|
|
|
338
421
|
end
|
|
339
422
|
```
|
|
340
423
|
|
|
341
|
-
|
|
424
|
+
## Matchers
|
|
342
425
|
|
|
343
|
-
|
|
344
|
-
|
|
426
|
+
Several domain-specific matchers are provided to each of the example group
|
|
427
|
+
types. Most simply delegate to their equivalent Rails' assertions.
|
|
345
428
|
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
429
|
+
### `be_a_new`
|
|
430
|
+
|
|
431
|
+
- Available in all specs
|
|
432
|
+
- Primarily intended for controller specs
|
|
349
433
|
|
|
350
434
|
```ruby
|
|
351
435
|
expect(object).to be_a_new(Widget)
|
|
352
436
|
```
|
|
353
437
|
|
|
354
|
-
|
|
355
438
|
Passes if the object is a `Widget` and returns true for `new_record?`
|
|
356
439
|
|
|
357
|
-
|
|
358
|
-
* Delegates to Rails' assert_template.
|
|
359
|
-
* Available in request, controller, and view specs.
|
|
440
|
+
### `render_template`
|
|
360
441
|
|
|
361
|
-
|
|
442
|
+
- Delegates to Rails' `assert_template`
|
|
443
|
+
- Available in request, controller, and view specs
|
|
444
|
+
|
|
445
|
+
In request and controller specs, apply to the `response` object:
|
|
362
446
|
|
|
363
447
|
```ruby
|
|
364
448
|
expect(response).to render_template("new")
|
|
365
449
|
```
|
|
366
450
|
|
|
367
|
-
In view specs, apply to the view object:
|
|
451
|
+
In view specs, apply to the `view` object:
|
|
368
452
|
|
|
369
453
|
```ruby
|
|
370
454
|
expect(view).to render_template(:partial => "_form", :locals => { :widget => widget } )
|
|
371
455
|
```
|
|
372
456
|
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
457
|
+
### `redirect_to`
|
|
458
|
+
|
|
459
|
+
- Delegates to `assert_redirect`
|
|
460
|
+
- Available in request and controller specs
|
|
376
461
|
|
|
377
462
|
```ruby
|
|
378
463
|
expect(response).to redirect_to(widgets_path)
|
|
379
464
|
```
|
|
380
465
|
|
|
381
|
-
|
|
466
|
+
### `route_to`
|
|
382
467
|
|
|
383
|
-
|
|
384
|
-
|
|
468
|
+
- Delegates to Rails' `assert_routing`
|
|
469
|
+
- Available in routing and controller specs
|
|
385
470
|
|
|
386
471
|
```ruby
|
|
387
472
|
expect(:get => "/widgets").to route_to(:controller => "widgets", :action => "index")
|
|
388
473
|
```
|
|
389
474
|
|
|
390
|
-
|
|
475
|
+
### `be_routable`
|
|
391
476
|
|
|
392
477
|
Passes if the path is recognized by Rails' routing. This is primarily intended
|
|
393
|
-
to be used with `not_to` to specify routes
|
|
478
|
+
to be used with `not_to` to specify standard CRUD routes which should not be
|
|
479
|
+
routable.
|
|
394
480
|
|
|
395
481
|
```ruby
|
|
396
482
|
expect(:get => "/widgets/1/edit").not_to be_routable
|
|
397
483
|
```
|
|
398
484
|
|
|
399
|
-
|
|
485
|
+
### `have_http_status`
|
|
400
486
|
|
|
401
|
-
|
|
402
|
-
|
|
487
|
+
- Passes if `response` has a matching HTTP status code
|
|
488
|
+
- The following symbolic status codes are allowed:
|
|
489
|
+
- `Rack::Utils::SYMBOL_TO_STATUS_CODE`
|
|
490
|
+
- One of the defined `ActionDispatch::TestResponse` aliases:
|
|
491
|
+
- `:error`
|
|
492
|
+
- `:missing`
|
|
493
|
+
- `:redirect`
|
|
494
|
+
- `:success`
|
|
495
|
+
- Available in controller, feature, and request specs.
|
|
496
|
+
|
|
497
|
+
In controller and request specs, apply to the `response` object:
|
|
498
|
+
|
|
499
|
+
```ruby
|
|
500
|
+
expect(response).to have_http_status(201)
|
|
501
|
+
expect(response).not_to have_http_status(:created)
|
|
502
|
+
```
|
|
503
|
+
|
|
504
|
+
In feature specs, apply to the `page` object:
|
|
505
|
+
|
|
506
|
+
```ruby
|
|
507
|
+
expect(page).to have_http_status(:success)
|
|
508
|
+
```
|
|
509
|
+
|
|
510
|
+
## `rake` tasks
|
|
511
|
+
|
|
512
|
+
Several rake tasks are provided as a convience for working with RSpec. To run
|
|
513
|
+
the entire spec suite use `rake spec`. To run a subset of specs use the
|
|
514
|
+
associated type task, for example `rake spec:models`.
|
|
403
515
|
|
|
404
516
|
A full list of the available rake tasks can be seen by running `rake -T | grep
|
|
405
517
|
spec`.
|
|
406
518
|
|
|
407
|
-
|
|
519
|
+
### Customizing `rake` tasks
|
|
408
520
|
|
|
409
521
|
If you want to customize the behavior of `rake spec`, you may [define your own
|
|
410
522
|
task in the `Rakefile` for your
|
|
@@ -415,29 +527,14 @@ However, you must first clear the task that rspec-rails defined:
|
|
|
415
527
|
task("spec").clear
|
|
416
528
|
```
|
|
417
529
|
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
You can choose between webrat or capybara for simulating a browser, automating
|
|
421
|
-
a browser, or setting expectations using the matchers they supply. Just add
|
|
422
|
-
your preference to the Gemfile:
|
|
423
|
-
|
|
424
|
-
```ruby
|
|
425
|
-
gem "webrat"
|
|
426
|
-
# ... or ...
|
|
427
|
-
gem "capybara"
|
|
428
|
-
```
|
|
429
|
-
|
|
430
|
-
See [http://rubydoc.info/gems/rspec-rails/file/Capybara.md](http://rubydoc.info/gems/rspec-rails/file/Capybara.md)
|
|
431
|
-
for more info on Capybara integration.
|
|
432
|
-
|
|
433
|
-
# Contribute
|
|
530
|
+
## Contribute
|
|
434
531
|
|
|
435
532
|
See [http://github.com/rspec/rspec-dev](http://github.com/rspec/rspec-dev).
|
|
436
533
|
|
|
437
534
|
For `rspec-rails`-specific development information, see
|
|
438
|
-
[
|
|
535
|
+
[README_DEV](https://github.com/rspec/rspec-rails/blob/master/README_DEV.md).
|
|
439
536
|
|
|
440
|
-
|
|
537
|
+
## Also see
|
|
441
538
|
|
|
442
539
|
* [http://github.com/rspec/rspec](http://github.com/rspec/rspec)
|
|
443
540
|
* [http://github.com/rspec/rspec-core](http://github.com/rspec/rspec-core)
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
require '
|
|
1
|
+
require 'rails_helper'
|
|
2
2
|
|
|
3
3
|
<% module_namespacing do -%>
|
|
4
|
-
describe <%= class_name %>Controller do
|
|
4
|
+
RSpec.describe <%= class_name %>Controller, :type => :controller do
|
|
5
5
|
|
|
6
6
|
<% for action in actions -%>
|
|
7
|
-
describe "GET
|
|
7
|
+
describe "GET <%= action %>" do
|
|
8
8
|
it "returns http success" do
|
|
9
|
-
get
|
|
10
|
-
response.
|
|
9
|
+
get :<%= action %>
|
|
10
|
+
expect(response).to have_http_status(:success)
|
|
11
11
|
end
|
|
12
12
|
end
|
|
13
13
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
require '
|
|
1
|
+
require 'rails_helper'
|
|
2
2
|
|
|
3
|
-
describe "<%= file_name %>/<%= @action %>.html.<%= options[:template_engine] %>" do
|
|
3
|
+
RSpec.describe "<%= file_name %>/<%= @action %>.html.<%= options[:template_engine] %>", :type => :view do
|
|
4
4
|
pending "add some examples to (or delete) #{__FILE__}"
|
|
5
5
|
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
require 'generators/rspec'
|
|
2
|
+
|
|
3
|
+
module Rspec
|
|
4
|
+
module Generators
|
|
5
|
+
# @private
|
|
6
|
+
class FeatureGenerator < Base
|
|
7
|
+
class_option :feature_specs, :type => :boolean, :default => true, :desc => "Generate feature specs"
|
|
8
|
+
|
|
9
|
+
def generate_feature_spec
|
|
10
|
+
return unless options[:feature_specs]
|
|
11
|
+
|
|
12
|
+
template 'feature_spec.rb', File.join('spec/features', class_path, "#{table_name}_spec.rb") # file_name?
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require '
|
|
1
|
+
require 'rails_helper'
|
|
2
2
|
|
|
3
3
|
# Specs in this file have access to a helper object that includes
|
|
4
4
|
# the <%= class_name %>Helper. For example:
|
|
@@ -11,7 +11,7 @@ require 'spec_helper'
|
|
|
11
11
|
# end
|
|
12
12
|
# end
|
|
13
13
|
<% module_namespacing do -%>
|
|
14
|
-
describe <%= class_name %>Helper do
|
|
14
|
+
RSpec.describe <%= class_name %>Helper, :type => :helper do
|
|
15
15
|
pending "add some examples to (or delete) #{__FILE__}"
|
|
16
16
|
end
|
|
17
17
|
<% end -%>
|