rspec-rails 3.8.2 → 3.8.3
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 +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/Changelog.md +8 -1
- data/README.md +266 -498
- data/lib/generators/rspec/model/model_generator.rb +1 -1
- data/lib/rspec/rails/matchers/be_valid.rb +1 -1
- data/lib/rspec/rails/version.rb +1 -1
- metadata +4 -4
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b76ee007b0ff4daff8f82e2465b9113cab486c9c70f5d9a6e70640f978c36d8d
|
4
|
+
data.tar.gz: a1b3441077b5535eeba867fdebebccfc1839e4a4b7a41c6294511c37f178152d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 70976f62f63a766f1db09c523f9565f21dc023e29a1e4f27874d09614d7cf26313a7b6e44a2007220668eafb06f3fce8640ead665970d46fee0453a1f6fe5172
|
7
|
+
data.tar.gz: 1170e6543f4d19dec09c3e754d78ea9807aa7ec17156637dc79a4c3bf192d7306052b1d42e671c03475482d80d22f623dc9f8ceea492eebf5aa590864f86e557
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/Changelog.md
CHANGED
@@ -1,6 +1,13 @@
|
|
1
|
-
###
|
1
|
+
### 3.8.3 / 2019-10-03
|
2
2
|
[Full Changelog](http://github.com/rspec/rspec-rails/compare/v3.8.2...master)
|
3
3
|
|
4
|
+
Bug Fixes:
|
5
|
+
|
6
|
+
* Namespaced fixtures now generate a `/` seperated path rather than an `_`.
|
7
|
+
(@nxlith, #2077)
|
8
|
+
* Check the arity of `errors` before attempting to use it to generate the `be_valid`
|
9
|
+
error message. (Kevin Kuchta, #2096)
|
10
|
+
|
4
11
|
### 3.8.2 / 2019-01-13
|
5
12
|
[Full Changelog](http://github.com/rspec/rspec-rails/compare/v3.8.1...v3.8.2)
|
6
13
|
|
data/README.md
CHANGED
@@ -1,602 +1,370 @@
|
|
1
|
-
# rspec-rails [![Build Status]
|
2
|
-
**rspec-rails** is a testing framework for Rails 3.x, 4.x and 5.x.
|
1
|
+
# rspec-rails [![Build Status][]][travis-ci] [![Code Climate][]][code-climate] [![Gem Version][]](gem-version)
|
3
2
|
|
4
|
-
|
5
|
-
|
3
|
+
`rspec-rails` brings the [RSpec][] testing framework to [Ruby on Rails][]
|
4
|
+
as a drop-in alternative to its default testing framework, Minitest.
|
6
5
|
|
7
|
-
|
6
|
+
In RSpec, tests are not just scripts that verify your application code.
|
7
|
+
They’re also specifications (or _specs,_ for short):
|
8
|
+
detailed explanations of how the application is supposed to behave,
|
9
|
+
expressed in plain English.
|
8
10
|
|
9
|
-
|
10
|
-
`Gemfile`:
|
11
|
+
Use **[`rspec-rails` 1.x][]** for Rails 2.x.
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
[Build Status]: https://secure.travis-ci.org/rspec/rspec-rails.svg?branch=master
|
14
|
+
[travis-ci]: https://travis-ci.org/rspec/rspec-rails
|
15
|
+
[Code Climate]: https://codeclimate.com/github/rspec/rspec-rails.svg
|
16
|
+
[code-climate]: https://codeclimate.com/github/rspec/rspec-rails
|
17
|
+
[Gem Version]: https://badge.fury.io/rb/rspec-rails.svg
|
18
|
+
[gem-version]: https://badge.fury.io/rb/rspec-rails
|
19
|
+
[RSpec]: https://rspec.info/
|
20
|
+
[Ruby on Rails]: https://rubyonrails.org/
|
21
|
+
[`rspec-rails` 1.x]: https://github.com/dchelimsky/rspec-rails
|
17
22
|
|
18
|
-
|
19
|
-
RSpec repos as well. Add the following to your `Gemfile`:
|
23
|
+
## Installation
|
20
24
|
|
21
|
-
|
22
|
-
|
23
|
-
gem lib, :git => "https://github.com/rspec/#{lib}.git", :branch => 'master'
|
24
|
-
end
|
25
|
-
```
|
25
|
+
1. Add `rspec-rails` to **both** the `:development` and `:test` groups
|
26
|
+
of your app’s `Gemfile`:
|
26
27
|
|
27
|
-
|
28
|
+
```ruby
|
29
|
+
# Run against the latest stable release
|
30
|
+
group :development, :test do
|
31
|
+
gem 'rspec-rails', '~> 3.8'
|
32
|
+
end
|
28
33
|
|
29
|
-
|
30
|
-
|
31
|
-
|
34
|
+
# Or, run against the master branch
|
35
|
+
# (requires master-branch versions of all related RSpec libraries)
|
36
|
+
group :development, :test do
|
37
|
+
%w[rspec-core rspec-expectations rspec-mocks rspec-rails rspec-support].each do |lib|
|
38
|
+
gem lib, :git => "https://github.com/rspec/#{lib}.git", :branch => 'master'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
```
|
32
42
|
|
33
|
-
|
43
|
+
(Adding it to the `:development` group is not strictly necessary,
|
44
|
+
but without it, generators and rake tasks must be preceded by `RAILS_ENV=test`.)
|
34
45
|
|
35
|
-
|
36
|
-
rails generate rspec:install
|
37
|
-
```
|
46
|
+
2. Then, in your project directory:
|
38
47
|
|
39
|
-
|
48
|
+
```sh
|
49
|
+
# Download and install
|
50
|
+
$ bundle install
|
40
51
|
|
41
|
-
|
42
|
-
|
43
|
-
|
52
|
+
# Generate boilerplate configuration files
|
53
|
+
# (check the comments in each generated file for more information)
|
54
|
+
$ rails generate rspec:install
|
55
|
+
create .rspec
|
56
|
+
create spec
|
57
|
+
create spec/spec_helper.rb
|
58
|
+
create spec/rails_helper.rb
|
59
|
+
```
|
44
60
|
|
45
|
-
|
61
|
+
## Upgrading
|
46
62
|
|
47
|
-
|
63
|
+
If your project is already using an older version of `rspec-rails`,
|
64
|
+
upgrade to the latest version with:
|
48
65
|
|
49
|
-
```
|
50
|
-
bundle
|
66
|
+
```sh
|
67
|
+
$ bundle update rspec-rails
|
51
68
|
```
|
52
69
|
|
53
|
-
|
54
|
-
|
55
|
-
|
70
|
+
RSpec follows [semantic versioning](https://semver.org/),
|
71
|
+
which means that “major version” upgrades (_e.g.,_ 2.x → 3.x)
|
72
|
+
come with **breaking changes**.
|
73
|
+
If you’re upgrading from version 2.x or below,
|
74
|
+
read the [`rspec-rails` upgrade notes][] to find out what to watch out for.
|
56
75
|
|
57
|
-
|
76
|
+
Be sure to check the general [RSpec upgrade notes][] as well.
|
58
77
|
|
59
|
-
|
60
|
-
|
61
|
-
bundle exec rspec spec/models
|
78
|
+
[`rspec-rails` upgrade notes]: https://www.relishapp.com/rspec/rspec-rails/docs/upgrade
|
79
|
+
[RSpec upgrade notes]: https://relishapp.com/rspec/docs/upgrade
|
62
80
|
|
63
|
-
|
64
|
-
bundle exec rspec spec/controllers/accounts_controller_spec.rb
|
81
|
+
## Usage
|
65
82
|
|
66
|
-
|
67
|
-
bundle exec rspec spec/controllers/accounts_controller_spec.rb:8
|
68
|
-
```
|
83
|
+
### Creating boilerplate specs with `rails generate`
|
69
84
|
|
70
|
-
|
71
|
-
|
85
|
+
```sh
|
86
|
+
# RSpec hooks into built-in generators
|
87
|
+
$ rails generate model user
|
88
|
+
invoke active_record
|
89
|
+
create db/migrate/20181017040312_create_users.rb
|
90
|
+
create app/models/user.rb
|
91
|
+
invoke rspec
|
92
|
+
create spec/models/user_spec.rb
|
72
93
|
|
73
|
-
|
74
|
-
|
94
|
+
# RSpec also provides its own spec file generators
|
95
|
+
$ rails generate rspec:model user
|
96
|
+
create spec/models/user_spec.rb
|
75
97
|
|
98
|
+
# List all RSpec generators
|
99
|
+
$ rails generate --help | grep rspec
|
76
100
|
```
|
77
|
-
bundle binstubs rspec-core
|
78
|
-
```
|
79
|
-
|
80
|
-
### Upgrade Note
|
81
|
-
|
82
|
-
For detailed information on the general RSpec 3.x upgrade process see the
|
83
|
-
[RSpec Upgrade docs](https://relishapp.com/rspec/docs/upgrade).
|
84
|
-
|
85
|
-
There are three particular `rspec-rails` specific changes to be aware of:
|
86
101
|
|
87
|
-
|
88
|
-
2. [File-type inference disabled by default](https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#file-type-inference-disabled)
|
89
|
-
3. [Rails 4.x `ActiveRecord::Migration` pending migration checks](https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#pending-migration-checks)
|
90
|
-
4. Extraction of `stub_model` and `mock_model` to
|
91
|
-
[`rspec-activemodel-mocks`](https://github.com/rspec/rspec-activemodel-mocks)
|
92
|
-
5. In Rails 5.x, controller testing has been moved to its own gem which is [rails-controller-testing](https://github.com/rails/rails-controller-testing). Using `assigns` in your controller specs without adding this gem will no longer work.
|
93
|
-
6. `rspec-rails` now includes two helpers, `spec_helper.rb` and `rails_helper.rb`.
|
94
|
-
`spec_helper.rb` is the conventional RSpec configuration helper, whilst the
|
95
|
-
Rails specific loading and bootstrapping has moved to the `rails_helper.rb`
|
96
|
-
file. Rails specs now need this file required beforehand either at the top
|
97
|
-
of the specific file (recommended) or a common configuration location such
|
98
|
-
as your `.rspec` file.
|
102
|
+
### Running specs
|
99
103
|
|
100
|
-
|
101
|
-
|
102
|
-
|
104
|
+
```sh
|
105
|
+
# Default: Run all spec files (i.e., those matching spec/**/*_spec.rb)
|
106
|
+
$ bundle exec rspec
|
103
107
|
|
104
|
-
|
105
|
-
|
108
|
+
# Run all spec files in a single directory (recursively)
|
109
|
+
$ bundle exec rspec spec/models
|
106
110
|
|
107
|
-
|
111
|
+
# Run a single spec file
|
112
|
+
$ bundle exec rspec spec/controllers/accounts_controller_spec.rb
|
108
113
|
|
109
|
-
|
110
|
-
|
111
|
-
used.
|
114
|
+
# Run a single example from a spec file (by line number)
|
115
|
+
$ bundle exec rspec spec/controllers/accounts_controller_spec.rb:8
|
112
116
|
|
113
|
-
|
114
|
-
|
115
|
-
information, see [list of all
|
116
|
-
generators](https://www.relishapp.com/rspec/rspec-rails/docs/generators).
|
117
|
-
|
118
|
-
## Contributing
|
119
|
-
|
120
|
-
Once you've set up the environment, you'll need to cd into the working
|
121
|
-
directory of whichever repo you want to work in. From there you can run the
|
122
|
-
specs and cucumber features, and make patches.
|
123
|
-
|
124
|
-
NOTE: You do not need to use rspec-dev to work on a specific RSpec repo. You
|
125
|
-
can treat each RSpec repo as an independent project.
|
126
|
-
Please see the following files:
|
127
|
-
|
128
|
-
For `rspec-rails`-specific development information, see
|
129
|
-
|
130
|
-
- [Build details](BUILD_DETAIL.md)
|
131
|
-
- [Code of Conduct](CODE_OF_CONDUCT.md)
|
132
|
-
- [Detailed contributing guide](CONTRIBUTING.md)
|
133
|
-
- [Development setup guide](DEVELOPMENT.md)
|
134
|
-
|
135
|
-
|
136
|
-
## Model Specs
|
137
|
-
|
138
|
-
Use model specs to describe behavior of models (usually ActiveRecord-based) in
|
139
|
-
the application.
|
140
|
-
|
141
|
-
Model specs default to residing in the `spec/models` folder. Tagging any
|
142
|
-
context with the metadata `:type => :model` treats its examples as model
|
143
|
-
specs.
|
144
|
-
|
145
|
-
For example:
|
146
|
-
|
147
|
-
```ruby
|
148
|
-
require "rails_helper"
|
149
|
-
|
150
|
-
RSpec.describe Post, :type => :model do
|
151
|
-
context "with 2 or more comments" do
|
152
|
-
it "orders them in reverse chronologically" do
|
153
|
-
post = Post.create!
|
154
|
-
comment1 = post.comments.create!(:body => "first comment")
|
155
|
-
comment2 = post.comments.create!(:body => "second comment")
|
156
|
-
expect(post.reload.comments).to eq([comment2, comment1])
|
157
|
-
end
|
158
|
-
end
|
159
|
-
end
|
117
|
+
# See all options for running specs
|
118
|
+
$ bundle exec rspec --help
|
160
119
|
```
|
161
120
|
|
162
|
-
|
163
|
-
|
121
|
+
**Optional:** If `bundle exec rspec` is too verbose for you,
|
122
|
+
you can generate a binstub at `bin/rspec`
|
123
|
+
and use that instead (Rails 4+ only):
|
164
124
|
|
165
|
-
|
125
|
+
```sh
|
126
|
+
$ bundle binstubs rspec-core
|
127
|
+
```
|
166
128
|
|
167
|
-
|
168
|
-
specifically, the HTTP response to be issued for a given request (a.k.a.
|
169
|
-
integration tests). Since such client-facing behavior encompasses controller
|
170
|
-
actions, this is the type of spec to use for controller testing.
|
129
|
+
## RSpec DSL Basics (or, how do I write a spec?)
|
171
130
|
|
172
|
-
|
173
|
-
|
174
|
-
:request` treats its examples as request specs.
|
175
|
-
|
176
|
-
Request specs mix in behavior from
|
177
|
-
[ActionDispatch::Integration::Runner](http://api.rubyonrails.org/classes/ActionDispatch/Integration/Runner.html),
|
178
|
-
which is the basis for [Rails' integration
|
179
|
-
tests](http://guides.rubyonrails.org/testing.html#integration-testing).
|
131
|
+
In RSpec, application behavior is described
|
132
|
+
**first in (almost) plain English, then again in test code**, like so:
|
180
133
|
|
181
134
|
```ruby
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
user = User.create!(:username => "jdoe", :password => "secret")
|
187
|
-
get "/login"
|
188
|
-
assert_select "form.login" do
|
189
|
-
assert_select "input[name=?]", "username"
|
190
|
-
assert_select "input[name=?]", "password"
|
191
|
-
assert_select "input[type=?]", "submit"
|
135
|
+
RSpec.describe 'Post' do #
|
136
|
+
context 'before publication' do # (almost) plain English
|
137
|
+
it 'cannot have comments' do #
|
138
|
+
expect { Post.create.comments.create! }.to raise_error(ActiveRecord::RecordInvalid) # test code
|
192
139
|
end
|
193
|
-
|
194
|
-
post "/login", :username => "jdoe", :password => "secret"
|
195
|
-
assert_select ".header .username", :text => "jdoe"
|
196
|
-
end
|
197
|
-
end
|
198
|
-
```
|
199
|
-
|
200
|
-
The above example uses only standard Rails and RSpec APIs, but many
|
201
|
-
RSpec/Rails users like to use extension libraries like
|
202
|
-
[FactoryBot](https://github.com/thoughtbot/factory_bot) and
|
203
|
-
[Capybara](https://github.com/jnicklas/capybara):
|
204
|
-
|
205
|
-
```ruby
|
206
|
-
require 'rails_helper'
|
207
|
-
|
208
|
-
RSpec.describe "home page", :type => :request do
|
209
|
-
it "displays the user's username after successful login" do
|
210
|
-
user = FactoryBot.create(:user, :username => "jdoe", :password => "secret")
|
211
|
-
visit "/login"
|
212
|
-
fill_in "Username", :with => "jdoe"
|
213
|
-
fill_in "Password", :with => "secret"
|
214
|
-
click_button "Log in"
|
215
|
-
|
216
|
-
expect(page).to have_selector(".header .username", :text => "jdoe")
|
217
140
|
end
|
218
141
|
end
|
219
142
|
```
|
220
143
|
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
Among other benefits, Capybara binds the form post to the generated HTML, which
|
226
|
-
means we don't need to specify them separately. Note that Capybara's DSL as
|
227
|
-
shown is, by default, only available in specs in the spec/features directory.
|
228
|
-
For more information, see the [Capybara integration
|
229
|
-
docs](http://rubydoc.info/gems/rspec-rails/file/Capybara.md).
|
230
|
-
|
231
|
-
There are several other Ruby libs that implement the factory pattern or provide
|
232
|
-
a DSL for request specs (a.k.a. acceptance or integration specs), but
|
233
|
-
FactoryBot and Capybara seem to be the most widely used. Whether you choose
|
234
|
-
these or other libs, we strongly recommend using something for each of these
|
235
|
-
roles.
|
236
|
-
|
237
|
-
For more information, see [cucumber scenarios for request
|
238
|
-
specs](https://relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec).
|
239
|
-
|
240
|
-
## Controller Specs
|
241
|
-
|
242
|
-
Controller specs can be used to describe the behavior of Rails controllers. As
|
243
|
-
of version 3.5, however, controller specs are discouraged in favor of request
|
244
|
-
specs (which also focus largely on controllers, but capture other critical
|
245
|
-
aspects of application behavior as well). Controller specs will continue to be
|
246
|
-
supported until at least version 4.0 (see the [release
|
247
|
-
notes](http://rspec.info/blog/2016/07/rspec-3-5-has-been-released/#rails-support-for-rails-5)
|
248
|
-
for details).
|
249
|
-
|
250
|
-
For more information, see [cucumber scenarios for controller
|
251
|
-
specs](https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs).
|
252
|
-
|
253
|
-
## Feature Specs
|
254
|
-
|
255
|
-
Feature specs test your application from the outside by simulating a browser.
|
256
|
-
[`capybara`](https://github.com/jnicklas/capybara) is used to manage the
|
257
|
-
simulated browser.
|
258
|
-
|
259
|
-
Feature specs default to residing in the `spec/features` folder. Tagging any
|
260
|
-
context with the metadata `:type => :feature` treats its examples as feature
|
261
|
-
specs.
|
144
|
+
Running `rspec` will execute this test code,
|
145
|
+
and then use the plain-English descriptions
|
146
|
+
to generate a report of where the application
|
147
|
+
conforms to (or fails to meet) the spec:
|
262
148
|
|
263
|
-
Feature specs mix in functionality from the capybara gem, thus they require
|
264
|
-
`capybara` to use. To use feature specs, add `capybara` to the `Gemfile`:
|
265
|
-
|
266
|
-
```ruby
|
267
|
-
gem "capybara"
|
268
149
|
```
|
150
|
+
$ rspec --format documentation spec/models/post_spec.rb
|
269
151
|
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
## Mailer specs
|
152
|
+
Post
|
153
|
+
before publication
|
154
|
+
cannot have comments
|
274
155
|
|
275
|
-
|
276
|
-
`:type => :mailer` to any context makes its examples be treated as mailer specs.
|
156
|
+
Failures:
|
277
157
|
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
158
|
+
1) Post before publication cannot have comments
|
159
|
+
Failure/Error: expect { Post.create.comments.create! }.to raise_error(ActiveRecord::RecordInvalid)
|
160
|
+
expected ActiveRecord::RecordInvalid but nothing was raised
|
161
|
+
# ./spec/models/post.rb:4:in `block (3 levels) in <top (required)>'
|
282
162
|
|
283
|
-
|
284
|
-
|
285
|
-
let(:mail) { Notifications.signup }
|
163
|
+
Finished in 0.00527 seconds (files took 0.29657 seconds to load)
|
164
|
+
1 example, 1 failure
|
286
165
|
|
287
|
-
|
288
|
-
expect(mail.subject).to eq("Signup")
|
289
|
-
expect(mail.to).to eq(["to@example.org"])
|
290
|
-
expect(mail.from).to eq(["from@example.com"])
|
291
|
-
end
|
166
|
+
Failed examples:
|
292
167
|
|
293
|
-
|
294
|
-
expect(mail.body.encoded).to match("Hi")
|
295
|
-
end
|
296
|
-
end
|
297
|
-
end
|
168
|
+
rspec ./spec/models/post_spec.rb:3 # Post before publication cannot have comments
|
298
169
|
```
|
299
170
|
|
300
|
-
For
|
301
|
-
]
|
302
|
-
|
303
|
-
## Job specs
|
304
|
-
|
305
|
-
Tagging a context with the metadata `:type => :job` treats its examples as job
|
306
|
-
specs. Typically these specs will live in `spec/jobs`.
|
307
|
-
|
308
|
-
```ruby
|
309
|
-
require 'rails_helper'
|
310
|
-
|
311
|
-
RSpec.describe UploadBackupsJob, :type => :job do
|
312
|
-
describe "#perform_later" do
|
313
|
-
it "uploads a backup" do
|
314
|
-
ActiveJob::Base.queue_adapter = :test
|
315
|
-
UploadBackupsJob.perform_later('backup')
|
316
|
-
expect(UploadBackupsJob).to have_been_enqueued
|
317
|
-
end
|
318
|
-
end
|
319
|
-
end
|
320
|
-
```
|
321
|
-
|
322
|
-
For more information, see the [cucumber scenarios for job specs
|
323
|
-
](https://relishapp.com/rspec/rspec-rails/docs/job-specs).
|
324
|
-
|
325
|
-
## View specs
|
171
|
+
For an in-depth look at the RSpec DSL, including lots of examples,
|
172
|
+
read the official Cucumber documentation for [RSpec Core][].
|
326
173
|
|
327
|
-
|
328
|
-
with the metadata `:type => :view` treats its examples as view specs.
|
174
|
+
[RSpec Core]: https://relishapp.com/rspec/rspec-core/docs
|
329
175
|
|
330
|
-
|
176
|
+
### Helpful Rails Matchers
|
331
177
|
|
332
|
-
|
333
|
-
|
178
|
+
In RSpec, assertions are called _expectations,_
|
179
|
+
and every expectation is built around a _matcher._
|
180
|
+
When you `expect(a).to eq(b)`, you’re using the `eq` matcher.
|
334
181
|
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
render
|
339
|
-
expect(view).to render_template(:partial => "_event", :count => 2)
|
340
|
-
end
|
341
|
-
end
|
182
|
+
In addition to [the matchers that come standard in RSpec][],
|
183
|
+
here are some extras that make it easier
|
184
|
+
to test the various parts of a Rails system:
|
342
185
|
|
343
|
-
RSpec
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
186
|
+
| RSpec matcher | Delegates to | Available in | Notes |
|
187
|
+
| ------------------------ | ----------------- | ------------------------------- | -------------------------------------------------------- |
|
188
|
+
| [`be_a_new`][] | | all | primarily intended for controller specs |
|
189
|
+
| [`render_template`][] | `assert_template` | request / controller / view | use with `expect(response).to` |
|
190
|
+
| [`redirect_to`][] | `assert_redirect` | request / controller | use with `expect(response).to` |
|
191
|
+
| [`route_to`] | `assert_routing` | routing / controller | replaces `route_for` from version 1.x |
|
192
|
+
| [`be_routable`] | | routing / controller | usu. for `expect(...).not_to be_routable` |
|
193
|
+
| [`have_http_status`][] | | request / controller / feature | |
|
194
|
+
| [`match_array`][] | | all | for comparing arrays of ActiveRecord objects |
|
195
|
+
| [`have_been_enqueued`][] | | all | requires config: `ActiveJob::Base.queue_adapter = :test` |
|
196
|
+
| [`have_enqueued_job`][] | | all | requires config: `ActiveJob::Base.queue_adapter = :test` |
|
351
197
|
|
352
|
-
|
353
|
-
template. e.g. if the template is `events/index.html.erb` then:
|
198
|
+
Follow the links above for examples of how each matcher is used.
|
354
199
|
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
200
|
+
[the matchers that come standard in RSpec]: https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers
|
201
|
+
[`be_a_new`]: https://relishapp.com/rspec/rspec-rails/docs/matchers/be-a-new-matcher
|
202
|
+
[`render_template`]: https://relishapp.com/rspec/rspec-rails/docs/matchers/render-template-matcher
|
203
|
+
[`redirect_to`]: https://relishapp.com/rspec/rspec-rails/docs/matchers/redirect-to-matcher
|
204
|
+
[`route_to`]: https://relishapp.com/rspec/rspec-rails/docs/routing-specs/route-to-matcher
|
205
|
+
[`be_routable`]: https://relishapp.com/rspec/rspec-rails/docs/routing-specs/be-routable-matcher
|
206
|
+
[`have_http_status`]: https://relishapp.com/rspec/rspec-rails/docs/matchers/have-http-status-matcher
|
207
|
+
[`match_array`]: https://relishapp.com/rspec/rspec-rails/docs/matchers/activerecord-relation-match-array
|
208
|
+
[`have_been_enqueued`]: https://relishapp.com/rspec/rspec-rails/docs/matchers/have-been-enqueued-matcher
|
209
|
+
[`have_enqueued_job`]: https://relishapp.com/rspec/rspec-rails/docs/matchers/have-enqueued-job-matcher
|
359
210
|
|
360
|
-
|
361
|
-
spec'ing a partial that is included across different controllers, you _may_
|
362
|
-
need to override these values before rendering the view.
|
211
|
+
### What else does RSpec Rails add?
|
363
212
|
|
364
|
-
|
365
|
-
|
213
|
+
For a comprehensive look at RSpec Rails’ features,
|
214
|
+
read the [official Cucumber documentation][].
|
366
215
|
|
367
|
-
|
368
|
-
render :template => "events/show", :layout => "layouts/application"
|
369
|
-
```
|
216
|
+
[official Cucumber documentation]: https://relishapp.com/rspec/rspec-rails/docs
|
370
217
|
|
371
|
-
|
218
|
+
## What tests should I write?
|
372
219
|
|
373
|
-
|
220
|
+
RSpec Rails defines ten different _types_ of specs
|
221
|
+
for testing different parts of a typical Rails application.
|
222
|
+
Each one inherits from one of Rails’ built-in `TestCase` classes,
|
223
|
+
meaning the helper methods provided by default in Rails tests
|
224
|
+
are available in RSpec, as well.
|
374
225
|
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
226
|
+
| Spec type | Corresponding Rails test class |
|
227
|
+
| -------------- | -------------------------------- |
|
228
|
+
| [model][] | |
|
229
|
+
| [controller][] | [`ActionController::TestCase`][] |
|
230
|
+
| [mailer][] | `ActionMailer::TestCase` |
|
231
|
+
| [job][] | |
|
232
|
+
| [view][] | `ActionView::TestCase` |
|
233
|
+
| [routing][] | |
|
234
|
+
| [helper][] | `ActionView::TestCase` |
|
235
|
+
| [request][] | [`ActionDispatch::IntegrationTest`][] |
|
236
|
+
| [feature][] | |
|
237
|
+
| [system][] | [`ActionDispatch::SystemTestCase`][] |
|
379
238
|
|
380
|
-
|
381
|
-
|
239
|
+
Follow the links above to see examples of each spec type,
|
240
|
+
or for official Rails API documentation on the given `TestCase` class.
|
382
241
|
|
383
|
-
Note
|
384
|
-
|
385
|
-
|
386
|
-
|
242
|
+
> **Note: This is not a checklist.**
|
243
|
+
>
|
244
|
+
> Ask a hundred developers how to test an application,
|
245
|
+
> and you’ll get a hundred different answers.
|
246
|
+
>
|
247
|
+
> RSpec Rails provides thoughtfully selected features
|
248
|
+
> to encourage good testing practices, but there’s no “right” way to do it.
|
249
|
+
> Ultimately, it’s up to you to decide how your test suite will be composed.
|
250
|
+
|
251
|
+
When creating a spec file,
|
252
|
+
assign it a type in the top-level `describe` block, like so:
|
387
253
|
|
388
254
|
```ruby
|
389
|
-
|
390
|
-
render # @widget is available inside the view
|
391
|
-
```
|
392
|
-
|
393
|
-
RSpec doesn't officially support this pattern, which only works as a
|
394
|
-
side-effect of the inclusion of `ActionView::TestCase`. Be aware that it may be
|
395
|
-
made unavailable in the future.
|
396
|
-
|
397
|
-
#### Upgrade note
|
398
|
-
|
399
|
-
```ruby
|
400
|
-
# rspec-rails-1.x
|
401
|
-
assigns[key] = value
|
402
|
-
|
403
|
-
# rspec-rails-2.x+
|
404
|
-
assign(key, value)
|
405
|
-
```
|
255
|
+
# spec/models/user_spec.rb
|
406
256
|
|
407
|
-
|
408
|
-
|
409
|
-
This represents the rendered view.
|
410
|
-
|
411
|
-
```ruby
|
412
|
-
render
|
413
|
-
expect(rendered).to match /Some text expected to appear on the page/
|
257
|
+
RSpec.describe User, type: :model do
|
258
|
+
...
|
414
259
|
```
|
415
260
|
|
416
|
-
|
261
|
+
[request]: https://relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec
|
262
|
+
[feature]: https://www.relishapp.com/rspec/rspec-rails/docs/feature-specs/feature-spec
|
263
|
+
[system]: https://relishapp.com/rspec/rspec-rails/docs/system-specs/system-spec
|
264
|
+
[model]: https://www.relishapp.com/rspec/rspec-rails/docs/model-specs
|
265
|
+
[controller]: https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs
|
266
|
+
[mailer]: https://relishapp.com/rspec/rspec-rails/docs/mailer-specs
|
267
|
+
[job]: https://relishapp.com/rspec/rspec-rails/docs/job-specs/job-spec
|
268
|
+
[view]: https://www.relishapp.com/rspec/rspec-rails/docs/view-specs/view-spec
|
269
|
+
[routing]: https://www.relishapp.com/rspec/rspec-rails/docs/routing-specs
|
270
|
+
[helper]: https://www.relishapp.com/rspec/rspec-rails/docs/helper-specs/helper-spec
|
271
|
+
[`ActionDispatch::IntegrationTest`]: https://api.rubyonrails.org/classes/ActionDispatch/IntegrationTest.html
|
272
|
+
[`ActionDispatch::SystemTestCase`]: https://api.rubyonrails.org/classes/ActionDispatch/SystemTestCase.html
|
273
|
+
[`ActionController::TestCase`]: https://api.rubyonrails.org/classes/ActionController/TestCase.html
|
274
|
+
[in the appropriate folder]: https://relishapp.com/rspec/rspec-rails/docs/directory-structure
|
417
275
|
|
418
|
-
|
419
|
-
# rspec-rails-1.x
|
420
|
-
render
|
421
|
-
response.should xxx
|
276
|
+
### System specs, feature specs, request specs–what’s the difference?
|
422
277
|
|
423
|
-
|
424
|
-
|
425
|
-
rendered.should xxx
|
278
|
+
RSpec Rails provides some end-to-end (entire application) testing capability
|
279
|
+
to specify the interaction with the client.
|
426
280
|
|
427
|
-
|
428
|
-
render
|
429
|
-
expect(rendered).to xxx
|
430
|
-
```
|
281
|
+
#### System specs
|
431
282
|
|
432
|
-
|
283
|
+
Also called **acceptance tests**, **browser tests**, or **end-to-end tests**,
|
284
|
+
system specs test the application from the perspective of a _human client._
|
285
|
+
The test code walks through a user’s browser interactions,
|
433
286
|
|
434
|
-
|
435
|
-
|
436
|
-
specs.
|
287
|
+
* `visit '/login'`
|
288
|
+
* `fill_in 'Name', with: 'jdoe'`
|
437
289
|
|
438
|
-
|
439
|
-
require 'rails_helper'
|
440
|
-
|
441
|
-
RSpec.describe "routing to profiles", :type => :routing do
|
442
|
-
it "routes /profile/:username to profile#show for username" do
|
443
|
-
expect(:get => "/profiles/jsmith").to route_to(
|
444
|
-
:controller => "profiles",
|
445
|
-
:action => "show",
|
446
|
-
:username => "jsmith"
|
447
|
-
)
|
448
|
-
end
|
290
|
+
and the expectations revolve around page content.
|
449
291
|
|
450
|
-
|
451
|
-
expect(:get => "/profiles").not_to be_routable
|
452
|
-
end
|
453
|
-
end
|
454
|
-
```
|
292
|
+
* `expect(page).to have_text('Welcome')`
|
455
293
|
|
456
|
-
|
294
|
+
Because system specs are a wrapper around Rails’ built-in `SystemTestCase`,
|
295
|
+
they’re only available on Rails 5.1+.
|
296
|
+
(Feature specs serve the same purpose, but without this dependency.)
|
457
297
|
|
458
|
-
|
459
|
-
instead.
|
298
|
+
#### Feature specs
|
460
299
|
|
461
|
-
|
300
|
+
Before Rails introduced system testing facilities,
|
301
|
+
feature specs were the only spec type for end-to-end testing.
|
302
|
+
While the RSpec team now [officially recommends system specs][] instead,
|
303
|
+
feature specs are still fully supported, look basically identical,
|
304
|
+
and work on older versions of Rails.
|
462
305
|
|
463
|
-
|
464
|
-
|
465
|
-
|
306
|
+
On the other hand, feature specs require non-trivial configuration
|
307
|
+
to get some important features working,
|
308
|
+
like JavaScript testing or making sure each test runs with a fresh DB state.
|
309
|
+
With system specs, this configuration is provided out-of-the-box.
|
466
310
|
|
467
|
-
|
468
|
-
|
469
|
-
|
311
|
+
Like system specs, feature specs require the [Capybara][] gem.
|
312
|
+
Rails 5.1+ includes it by default as part of system tests,
|
313
|
+
but if you don’t have the luxury of upgrading,
|
314
|
+
be sure to add it to the `:test` group of your `Gemfile` first:
|
470
315
|
|
471
316
|
```ruby
|
472
|
-
|
473
|
-
|
474
|
-
RSpec.describe EventsHelper, :type => :helper do
|
475
|
-
describe "#link_to_event" do
|
476
|
-
it "displays the title, and formatted date" do
|
477
|
-
event = Event.new("Ruby Kaigi", Date.new(2010, 8, 27))
|
478
|
-
# helper is an instance of ActionView::Base configured with the
|
479
|
-
# EventsHelper and all of Rails' built-in helpers
|
480
|
-
expect(helper.link_to_event).to match /Ruby Kaigi, 27 Aug, 2010/
|
481
|
-
end
|
482
|
-
end
|
317
|
+
group :test do
|
318
|
+
gem "capybara"
|
483
319
|
end
|
484
320
|
```
|
485
321
|
|
486
|
-
|
487
|
-
|
488
|
-
Several domain-specific matchers are provided to each of the example group
|
489
|
-
types. Most simply delegate to their equivalent Rails' assertions.
|
490
|
-
|
491
|
-
### `be_a_new`
|
492
|
-
|
493
|
-
- Available in all specs
|
494
|
-
- Primarily intended for controller specs
|
495
|
-
|
496
|
-
```ruby
|
497
|
-
expect(object).to be_a_new(Widget)
|
498
|
-
```
|
499
|
-
|
500
|
-
Passes if the object is a `Widget` and returns true for `new_record?`
|
322
|
+
[officially recommends system specs]: https://rspec.info/blog/2017/10/rspec-3-7-has-been-released/#rails-actiondispatchsystemtest-integration-system-specs
|
323
|
+
[Capybara]: https://github.com/teamcapybara/capybara
|
501
324
|
|
502
|
-
|
325
|
+
#### Request specs
|
503
326
|
|
504
|
-
|
505
|
-
|
327
|
+
Request specs are for testing the application
|
328
|
+
from the perspective of a _machine client._
|
329
|
+
They begin with an HTTP request and end with the HTTP response,
|
330
|
+
so they’re faster than feature specs,
|
331
|
+
but do not examine your app’s UI or JavaScript.
|
506
332
|
|
507
|
-
|
333
|
+
Request specs provide a high-level alternative to controller specs.
|
334
|
+
In fact, as of RSpec 3.5, both the Rails and RSpec teams
|
335
|
+
[discourage directly testing controllers][]
|
336
|
+
in favor of functional tests like request specs.
|
508
337
|
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
In view specs, apply to the `view` object:
|
514
|
-
|
515
|
-
```ruby
|
516
|
-
expect(view).to render_template(:partial => "_form", :locals => { :widget => widget } )
|
517
|
-
```
|
518
|
-
|
519
|
-
### `redirect_to`
|
520
|
-
|
521
|
-
- Delegates to `assert_redirect`
|
522
|
-
- Available in request and controller specs
|
523
|
-
|
524
|
-
```ruby
|
525
|
-
expect(response).to redirect_to(widgets_path)
|
526
|
-
```
|
527
|
-
|
528
|
-
### `route_to`
|
529
|
-
|
530
|
-
- Delegates to Rails' `assert_routing`
|
531
|
-
- Available in routing and controller specs
|
532
|
-
|
533
|
-
```ruby
|
534
|
-
expect(:get => "/widgets").to route_to(:controller => "widgets", :action => "index")
|
535
|
-
```
|
536
|
-
|
537
|
-
### `be_routable`
|
538
|
-
|
539
|
-
Passes if the path is recognized by Rails' routing. This is primarily intended
|
540
|
-
to be used with `not_to` to specify standard CRUD routes which should not be
|
541
|
-
routable.
|
542
|
-
|
543
|
-
```ruby
|
544
|
-
expect(:get => "/widgets/1/edit").not_to be_routable
|
545
|
-
```
|
546
|
-
|
547
|
-
### `have_http_status`
|
548
|
-
|
549
|
-
- Passes if `response` has a matching HTTP status code
|
550
|
-
- The following symbolic status codes are allowed:
|
551
|
-
- `Rack::Utils::SYMBOL_TO_STATUS_CODE`
|
552
|
-
- One of the defined `ActionDispatch::TestResponse` aliases:
|
553
|
-
- `:error`
|
554
|
-
- `:missing`
|
555
|
-
- `:redirect`
|
556
|
-
- `:success`
|
557
|
-
- Available in controller, feature, and request specs.
|
558
|
-
|
559
|
-
In controller and request specs, apply to the `response` object:
|
338
|
+
When writing them, try to answer the question,
|
339
|
+
“For a given HTTP request (verb + path + parameters),
|
340
|
+
what HTTP response should the application return?”
|
560
341
|
|
561
|
-
|
562
|
-
expect(response).to have_http_status(201)
|
563
|
-
expect(response).not_to have_http_status(:created)
|
564
|
-
```
|
565
|
-
|
566
|
-
In feature specs, apply to the `page` object:
|
342
|
+
[discourage directly testing controllers]: https://rspec.info/blog/2016/07/rspec-3-5-has-been-released/#rails-support-for-rails-5
|
567
343
|
|
568
|
-
|
569
|
-
expect(page).to have_http_status(:success)
|
570
|
-
```
|
571
|
-
|
572
|
-
## `rake` tasks
|
573
|
-
|
574
|
-
Several rake tasks are provided as a convenience for working with RSpec. To run
|
575
|
-
the entire spec suite use `rake spec`. To run a subset of specs use the
|
576
|
-
associated type task, for example `rake spec:models`.
|
577
|
-
|
578
|
-
A full list of the available rake tasks can be seen by running `rake -T | grep
|
579
|
-
spec`.
|
344
|
+
## Contributing
|
580
345
|
|
581
|
-
|
346
|
+
- [Build details](BUILD_DETAIL.md)
|
347
|
+
- [Code of Conduct](CODE_OF_CONDUCT.md)
|
348
|
+
- [Detailed contributing guide](CONTRIBUTING.md)
|
582
349
|
|
583
|
-
|
584
|
-
|
585
|
-
project](https://www.relishapp.com/rspec/rspec-core/docs/command-line/rake-task).
|
586
|
-
However, you must first clear the task that rspec-rails defined:
|
350
|
+
Once you’ve cloned the repo and [set up the environment](DEVELOPMENT.md),
|
351
|
+
you can run the specs and Cucumber features, or submit a pull request.
|
587
352
|
|
588
|
-
|
589
|
-
task("spec").clear
|
590
|
-
```
|
353
|
+
## See Also
|
591
354
|
|
355
|
+
### RSpec base libraries
|
592
356
|
|
593
|
-
|
357
|
+
* <https://github.com/rspec/rspec>
|
358
|
+
* <https://github.com/rspec/rspec-core>
|
359
|
+
* <https://github.com/rspec/rspec-expectations>
|
360
|
+
* <https://github.com/rspec/rspec-mocks>
|
594
361
|
|
595
|
-
|
596
|
-
* [https://github.com/rspec/rspec-core](https://github.com/rspec/rspec-core)
|
597
|
-
* [https://github.com/rspec/rspec-expectations](https://github.com/rspec/rspec-expectations)
|
598
|
-
* [https://github.com/rspec/rspec-mocks](https://github.com/rspec/rspec-mocks)
|
362
|
+
### Recommended third-party extensions
|
599
363
|
|
600
|
-
|
364
|
+
* [FactoryBot](https://github.com/thoughtbot/factory_bot)
|
365
|
+
* [Capybara](https://github.com/jnicklas/capybara)
|
366
|
+
(Included by default in Rails 5.1+.
|
367
|
+
Note that [additional configuration is required][] to use the Capybara DSL
|
368
|
+
anywhere other than system specs and feature specs.)
|
601
369
|
|
602
|
-
|
370
|
+
[additional configuration is required]: https://rubydoc.info/gems/rspec-rails/file/Capybara.md
|
@@ -23,7 +23,7 @@ module Rspec
|
|
23
23
|
|
24
24
|
def create_fixture_file
|
25
25
|
return unless missing_fixture_replacement?
|
26
|
-
template 'fixtures.yml', File.join('spec/fixtures', "#{
|
26
|
+
template 'fixtures.yml', File.join('spec/fixtures', class_path, "#{(pluralize_table_names? ? plural_file_name : file_name)}.yml")
|
27
27
|
end
|
28
28
|
|
29
29
|
private
|
@@ -15,7 +15,7 @@ module RSpec
|
|
15
15
|
def failure_message
|
16
16
|
message = "expected #{actual.inspect} to be valid"
|
17
17
|
|
18
|
-
if actual.respond_to?(:errors)
|
18
|
+
if actual.respond_to?(:errors) && actual.method(:errors).arity < 1
|
19
19
|
errors = if actual.errors.respond_to?(:full_messages)
|
20
20
|
actual.errors.full_messages
|
21
21
|
else
|
data/lib/rspec/rails/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.8.
|
4
|
+
version: 3.8.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Chelimsky
|
@@ -44,7 +44,7 @@ cert_chain:
|
|
44
44
|
ZsVDj6a7lH3cNqtWXZxrb2wO38qV5AkYj8SQK7Hj3/Yui9myUX3crr+PdetazSqQ
|
45
45
|
F3MdtaDehhjC
|
46
46
|
-----END CERTIFICATE-----
|
47
|
-
date: 2019-
|
47
|
+
date: 2019-10-09 00:00:00.000000000 Z
|
48
48
|
dependencies:
|
49
49
|
- !ruby/object:Gem::Dependency
|
50
50
|
name: activesupport
|
@@ -278,7 +278,7 @@ licenses:
|
|
278
278
|
- MIT
|
279
279
|
metadata:
|
280
280
|
bug_tracker_uri: https://github.com/rspec/rspec-rails/issues
|
281
|
-
changelog_uri: https://github.com/rspec/rspec-rails/blob/v3.8.
|
281
|
+
changelog_uri: https://github.com/rspec/rspec-rails/blob/v3.8.3/Changelog.md
|
282
282
|
documentation_uri: https://rspec.info/documentation/
|
283
283
|
mailing_list_uri: https://groups.google.com/forum/#!forum/rspec
|
284
284
|
source_code_uri: https://github.com/rspec/rspec-rails
|
@@ -298,7 +298,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
298
298
|
- !ruby/object:Gem::Version
|
299
299
|
version: '0'
|
300
300
|
requirements: []
|
301
|
-
rubygems_version: 3.0.
|
301
|
+
rubygems_version: 3.0.6
|
302
302
|
signing_key:
|
303
303
|
specification_version: 4
|
304
304
|
summary: RSpec for Rails
|
metadata.gz.sig
CHANGED
Binary file
|