rspec-rails 3.8.2 → 5.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/Capybara.md +5 -54
  4. data/Changelog.md +284 -74
  5. data/README.md +278 -500
  6. data/lib/generators/rspec/channel/channel_generator.rb +12 -0
  7. data/lib/generators/rspec/{observer/templates/observer_spec.rb → channel/templates/channel_spec.rb.erb} +1 -1
  8. data/lib/generators/rspec/controller/controller_generator.rb +22 -5
  9. data/lib/generators/rspec/controller/templates/request_spec.rb +19 -0
  10. data/lib/generators/rspec/controller/templates/routing_spec.rb +13 -0
  11. data/lib/generators/rspec/feature/feature_generator.rb +2 -2
  12. data/lib/generators/rspec/generator/generator_generator.rb +24 -0
  13. data/lib/generators/rspec/generator/templates/generator_spec.rb +6 -0
  14. data/lib/generators/rspec/helper/helper_generator.rb +1 -1
  15. data/lib/generators/rspec/install/install_generator.rb +4 -4
  16. data/lib/generators/rspec/install/templates/spec/rails_helper.rb +18 -13
  17. data/lib/generators/rspec/integration/integration_generator.rb +3 -3
  18. data/lib/generators/rspec/job/job_generator.rb +2 -1
  19. data/lib/generators/rspec/job/templates/job_spec.rb.erb +1 -1
  20. data/lib/generators/rspec/mailbox/mailbox_generator.rb +14 -0
  21. data/lib/generators/rspec/mailbox/templates/mailbox_spec.rb.erb +7 -0
  22. data/lib/generators/rspec/mailer/mailer_generator.rb +2 -1
  23. data/lib/generators/rspec/mailer/templates/mailer_spec.rb +2 -2
  24. data/lib/generators/rspec/mailer/templates/preview.rb +1 -1
  25. data/lib/generators/rspec/model/model_generator.rb +6 -5
  26. data/lib/generators/rspec/model/templates/fixtures.yml +1 -1
  27. data/lib/generators/rspec/request/request_generator.rb +1 -1
  28. data/lib/generators/rspec/scaffold/scaffold_generator.rb +33 -19
  29. data/lib/generators/rspec/scaffold/templates/api_controller_spec.rb +13 -49
  30. data/lib/generators/rspec/scaffold/templates/api_request_spec.rb +131 -0
  31. data/lib/generators/rspec/scaffold/templates/controller_spec.rb +10 -58
  32. data/lib/generators/rspec/scaffold/templates/edit_spec.rb +1 -5
  33. data/lib/generators/rspec/scaffold/templates/index_spec.rb +2 -2
  34. data/lib/generators/rspec/scaffold/templates/new_spec.rb +1 -5
  35. data/lib/generators/rspec/scaffold/templates/request_spec.rb +138 -0
  36. data/lib/generators/rspec/scaffold/templates/routing_spec.rb +8 -10
  37. data/lib/generators/rspec/scaffold/templates/show_spec.rb +1 -1
  38. data/lib/generators/rspec/system/system_generator.rb +24 -0
  39. data/lib/generators/rspec/system/templates/system_spec.rb +9 -0
  40. data/lib/generators/rspec/view/view_generator.rb +2 -2
  41. data/lib/generators/rspec.rb +0 -6
  42. data/lib/rspec/rails/adapters.rb +11 -76
  43. data/lib/rspec/rails/configuration.rb +82 -37
  44. data/lib/rspec/rails/example/channel_example_group.rb +93 -0
  45. data/lib/rspec/rails/example/controller_example_group.rb +5 -4
  46. data/lib/rspec/rails/example/feature_example_group.rb +6 -26
  47. data/lib/rspec/rails/example/helper_example_group.rb +2 -9
  48. data/lib/rspec/rails/example/mailbox_example_group.rb +80 -0
  49. data/lib/rspec/rails/example/mailer_example_group.rb +2 -2
  50. data/lib/rspec/rails/example/rails_example_group.rb +1 -1
  51. data/lib/rspec/rails/example/request_example_group.rb +1 -4
  52. data/lib/rspec/rails/example/system_example_group.rb +29 -12
  53. data/lib/rspec/rails/example/view_example_group.rb +38 -27
  54. data/lib/rspec/rails/example.rb +2 -0
  55. data/lib/rspec/rails/extensions/active_record/proxy.rb +5 -10
  56. data/lib/rspec/rails/feature_check.rb +12 -29
  57. data/lib/rspec/rails/file_fixture_support.rb +9 -11
  58. data/lib/rspec/rails/fixture_file_upload_support.rb +33 -17
  59. data/lib/rspec/rails/fixture_support.rb +35 -32
  60. data/lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb +170 -0
  61. data/lib/rspec/rails/matchers/action_cable/have_streams.rb +58 -0
  62. data/lib/rspec/rails/matchers/action_cable.rb +65 -0
  63. data/lib/rspec/rails/matchers/action_mailbox.rb +73 -0
  64. data/lib/rspec/rails/matchers/active_job.rb +170 -22
  65. data/lib/rspec/rails/matchers/base_matcher.rb +179 -0
  66. data/lib/rspec/rails/matchers/be_a_new.rb +1 -1
  67. data/lib/rspec/rails/matchers/be_new_record.rb +1 -1
  68. data/lib/rspec/rails/matchers/be_valid.rb +1 -1
  69. data/lib/rspec/rails/matchers/have_enqueued_mail.rb +226 -0
  70. data/lib/rspec/rails/matchers/have_http_status.rb +20 -25
  71. data/lib/rspec/rails/matchers/have_rendered.rb +2 -1
  72. data/lib/rspec/rails/matchers/redirect_to.rb +1 -1
  73. data/lib/rspec/rails/matchers/relation_match_array.rb +1 -1
  74. data/lib/rspec/rails/matchers/routing_matchers.rb +14 -14
  75. data/lib/rspec/rails/matchers.rb +11 -0
  76. data/lib/rspec/rails/tasks/rspec.rake +7 -17
  77. data/lib/rspec/rails/vendor/capybara.rb +10 -15
  78. data/lib/rspec/rails/version.rb +1 -1
  79. data/lib/rspec/rails/view_path_builder.rb +1 -1
  80. data/lib/rspec/rails/view_rendering.rb +15 -4
  81. data/lib/rspec-rails.rb +13 -16
  82. data.tar.gz.sig +0 -0
  83. metadata +64 -34
  84. metadata.gz.sig +0 -0
  85. data/lib/generators/rspec/observer/observer_generator.rb +0 -13
data/README.md CHANGED
@@ -1,602 +1,380 @@
1
- # rspec-rails [![Build Status](https://secure.travis-ci.org/rspec/rspec-rails.svg?branch=master)](http://travis-ci.org/rspec/rspec-rails) [![Code Climate](https://img.shields.io/codeclimate/github/rspec/rspec-rails.svg)](https://codeclimate.com/github/rspec/rspec-rails)
2
- **rspec-rails** is a testing framework for Rails 3.x, 4.x and 5.x.
3
-
4
- Use **[rspec-rails 1.x](http://github.com/dchelimsky/rspec-rails)** for Rails
5
- 2.x.
1
+ # rspec-rails [![Code Climate][]][code-climate] [![Gem Version][]][gem-version]
2
+
3
+ `rspec-rails` brings the [RSpec][] testing framework to [Ruby on Rails][]
4
+ as a drop-in alternative to its default testing framework, Minitest.
5
+
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.
10
+
11
+ According to [RSpec Rails new versioning strategy][] use:
12
+ * **[`rspec-rails` 5.x][]** for Rails 6.x.
13
+ * **[`rspec-rails` 4.x][]** for Rails from 5.x or 6.x.
14
+ * **[`rspec-rails` 3.x][]** for Rails earlier than 5.0.
15
+ * **[`rspec-rails` 1.x][]** for Rails 2.x.
16
+
17
+ [Build Status]: https://secure.travis-ci.org/rspec/rspec-rails.svg?branch=main
18
+ [travis-ci]: https://travis-ci.org/rspec/rspec-rails
19
+ [Code Climate]: https://codeclimate.com/github/rspec/rspec-rails.svg
20
+ [code-climate]: https://codeclimate.com/github/rspec/rspec-rails
21
+ [Gem Version]: https://badge.fury.io/rb/rspec-rails.svg
22
+ [gem-version]: https://badge.fury.io/rb/rspec-rails
23
+ [RSpec]: https://rspec.info/
24
+ [Ruby on Rails]: https://rubyonrails.org/
25
+ [`rspec-rails` 1.x]: https://github.com/dchelimsky/rspec-rails
26
+ [`rspec-rails` 3.x]: https://github.com/rspec/rspec-rails/tree/3-9-maintenance
27
+ [`rspec-rails` 4.x]: https://github.com/rspec/rspec-rails/tree/4-1-maintenance
28
+ [`rspec-rails` 5.x]: https://github.com/rspec/rspec-rails/tree/5-0-maintenance
29
+ [RSpec Rails new versioning strategy]: https://github.com/rspec/rspec-rails/blob/main/rfcs/versioning-strategy.md
6
30
 
7
31
  ## Installation
8
32
 
9
- Add `rspec-rails` to **both** the `:development` and `:test` groups in the
10
- `Gemfile`:
11
-
12
- ```ruby
13
- group :development, :test do
14
- gem 'rspec-rails', '~> 3.7'
15
- end
16
- ```
17
-
18
- Want to run against the `master` branch? You'll need to include the dependent
19
- RSpec repos as well. Add the following to your `Gemfile`:
33
+ **IMPORTANT** This README / branch refers to the current development build.
34
+ See the [`5-0-maintenance` branch on Github](https://github.com/rspec/rspec-rails/tree/5-0-maintenance) if you want or require the latest stable release.
20
35
 
21
- ```ruby
22
- %w[rspec-core rspec-expectations rspec-mocks rspec-rails rspec-support].each do |lib|
23
- gem lib, :git => "https://github.com/rspec/#{lib}.git", :branch => 'master'
24
- end
25
- ```
36
+ 1. Add `rspec-rails` to **both** the `:development` and `:test` groups
37
+ of your app’s `Gemfile`:
26
38
 
27
- Download and install by running:
39
+ ```ruby
40
+ # Run against this stable release
41
+ group :development, :test do
42
+ gem 'rspec-rails', '~> 5.0.0'
43
+ end
28
44
 
29
- ```
30
- bundle install
31
- ```
45
+ # Or, run against the main branch
46
+ # (requires main-branch versions of all related RSpec libraries)
47
+ group :development, :test do
48
+ %w[rspec-core rspec-expectations rspec-mocks rspec-rails rspec-support].each do |lib|
49
+ gem lib, git: "https://github.com/rspec/#{lib}.git", branch: 'main'
50
+ end
51
+ end
52
+ ```
32
53
 
33
- Initialize the `spec/` directory (where specs will reside) with:
54
+ (Adding it to the `:development` group is not strictly necessary,
55
+ but without it, generators and rake tasks must be preceded by `RAILS_ENV=test`.)
34
56
 
35
- ```
36
- rails generate rspec:install
37
- ```
57
+ 2. Then, in your project directory:
38
58
 
39
- This adds the following files which are used for configuration:
59
+ ```sh
60
+ # Download and install
61
+ $ bundle install
40
62
 
41
- - `.rspec`
42
- - `spec/spec_helper.rb`
43
- - `spec/rails_helper.rb`
63
+ # Generate boilerplate configuration files
64
+ # (check the comments in each generated file for more information)
65
+ $ rails generate rspec:install
66
+ create .rspec
67
+ create spec
68
+ create spec/spec_helper.rb
69
+ create spec/rails_helper.rb
70
+ ```
44
71
 
45
- Check the comments in each file for more information.
72
+ ## Upgrading
46
73
 
47
- Use the `rspec` command to run your specs:
74
+ If your project is already using an older version of `rspec-rails`,
75
+ upgrade to the latest version with:
48
76
 
49
- ```
50
- bundle exec rspec
77
+ ```sh
78
+ $ bundle update rspec-rails
51
79
  ```
52
80
 
53
- By default the above will run all `_spec.rb` files in the `spec` directory. For
54
- more details about this see the [RSpec spec file
55
- docs](https://www.relishapp.com/rspec/rspec-core/docs/spec-files).
81
+ RSpec follows [semantic versioning](https://semver.org/),
82
+ which means that “major version” upgrades (_e.g.,_ 2.x → 3.x)
83
+ come with **breaking changes**.
84
+ If you’re upgrading from version 2.x or below,
85
+ read the [`rspec-rails` upgrade notes][] to find out what to watch out for.
56
86
 
57
- To run only a subset of these specs use the following command:
87
+ Be sure to check the general [RSpec upgrade notes][] as well.
58
88
 
59
- ```
60
- # Run only model specs
61
- bundle exec rspec spec/models
89
+ [`rspec-rails` upgrade notes]: https://www.relishapp.com/rspec/rspec-rails/docs/upgrade
90
+ [RSpec upgrade notes]: https://relishapp.com/rspec/docs/upgrade
62
91
 
63
- # Run only specs for AccountsController
64
- bundle exec rspec spec/controllers/accounts_controller_spec.rb
92
+ ## Usage
65
93
 
66
- # Run only spec on line 8 of AccountsController
67
- bundle exec rspec spec/controllers/accounts_controller_spec.rb:8
68
- ```
94
+ ### Creating boilerplate specs with `rails generate`
69
95
 
70
- Specs can also be run via `rake spec`, though this command may be slower to
71
- start than the `rspec` command.
96
+ ```sh
97
+ # RSpec hooks into built-in generators
98
+ $ rails generate model user
99
+ invoke active_record
100
+ create db/migrate/20181017040312_create_users.rb
101
+ create app/models/user.rb
102
+ invoke rspec
103
+ create spec/models/user_spec.rb
72
104
 
73
- In Rails 4/5+, you may want to create a binstub for the `rspec` command so it can
74
- be run via `bin/rspec`:
105
+ # RSpec also provides its own spec file generators
106
+ $ rails generate rspec:model user
107
+ create spec/models/user_spec.rb
75
108
 
109
+ # List all RSpec generators
110
+ $ rails generate --help | grep rspec
76
111
  ```
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
112
 
87
- 1. [The default helper files created in RSpec 3.x have changed](https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#default-helper-files)
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.
113
+ ### Running specs
99
114
 
100
- Please see the [RSpec Rails Upgrade
101
- docs](https://www.relishapp.com/rspec/rspec-rails/docs/upgrade) for full
102
- details.
115
+ ```sh
116
+ # Default: Run all spec files (i.e., those matching spec/**/*_spec.rb)
117
+ $ bundle exec rspec
103
118
 
104
- **NOTE:** Generators run in RSpec 3.x will now require `rails_helper` instead
105
- of `spec_helper`.
119
+ # Run all spec files in a single directory (recursively)
120
+ $ bundle exec rspec spec/models
106
121
 
107
- ### Generators
122
+ # Run a single spec file
123
+ $ bundle exec rspec spec/controllers/accounts_controller_spec.rb
108
124
 
109
- Once installed, RSpec will generate spec files instead of Test::Unit test files
110
- when commands like `rails generate model` and `rails generate controller` are
111
- used.
125
+ # Run a single example from a spec file (by line number)
126
+ $ bundle exec rspec spec/controllers/accounts_controller_spec.rb:8
112
127
 
113
- You may also invoke RSpec generators independently. For instance,
114
- running `rails generate rspec:model` will generate a model spec. For more
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
128
+ # See all options for running specs
129
+ $ bundle exec rspec --help
160
130
  ```
161
131
 
162
- For more information, see [cucumber scenarios for model
163
- specs](https://www.relishapp.com/rspec/rspec-rails/docs/model-specs).
132
+ **Optional:** If `bundle exec rspec` is too verbose for you,
133
+ you can generate a binstub at `bin/rspec` and use that instead:
164
134
 
165
- ## Request Specs
135
+ ```sh
136
+ $ bundle binstubs rspec-core
137
+ ```
166
138
 
167
- Use request specs to describe the client-facing behavior of the application —
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.
139
+ ## RSpec DSL Basics (or, how do I write a spec?)
171
140
 
172
- Request specs default to residing in the `spec/requests`, `spec/api`, and
173
- `spec/integration` directories. Tagging any context with the metadata `:type =>
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).
141
+ In RSpec, application behavior is described
142
+ **first in (almost) plain English, then again in test code**, like so:
180
143
 
181
144
  ```ruby
182
- require 'rails_helper'
183
-
184
- RSpec.describe "home page", :type => :request do
185
- it "displays the user's username after successful login" do
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"
145
+ RSpec.describe 'Post' do #
146
+ context 'before publication' do # (almost) plain English
147
+ it 'cannot have comments' do #
148
+ expect { Post.create.comments.create! }.to raise_error(ActiveRecord::RecordInvalid) # test code
192
149
  end
193
-
194
- post "/login", :username => "jdoe", :password => "secret"
195
- assert_select ".header .username", :text => "jdoe"
196
150
  end
197
151
  end
198
152
  ```
199
153
 
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"
154
+ Running `rspec` will execute this test code,
155
+ and then use the plain-English descriptions
156
+ to generate a report of where the application
157
+ conforms to (or fails to meet) the spec:
215
158
 
216
- expect(page).to have_selector(".header .username", :text => "jdoe")
217
- end
218
- end
219
159
  ```
160
+ $ rspec --format documentation spec/models/post_spec.rb
220
161
 
221
- FactoryBot decouples this example from changes to validation requirements,
222
- which can be encoded into the underlying factory definition without requiring
223
- changes to this example.
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).
162
+ Post
163
+ before publication
164
+ cannot have comments
239
165
 
240
- ## Controller Specs
166
+ Failures:
241
167
 
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).
168
+ 1) Post before publication cannot have comments
169
+ Failure/Error: expect { Post.create.comments.create! }.to raise_error(ActiveRecord::RecordInvalid)
170
+ expected ActiveRecord::RecordInvalid but nothing was raised
171
+ # ./spec/models/post.rb:4:in `block (3 levels) in <top (required)>'
249
172
 
250
- For more information, see [cucumber scenarios for controller
251
- specs](https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs).
173
+ Finished in 0.00527 seconds (files took 0.29657 seconds to load)
174
+ 1 example, 1 failure
252
175
 
253
- ## Feature Specs
176
+ Failed examples:
254
177
 
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.
262
-
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"
178
+ rspec ./spec/models/post_spec.rb:3 # Post before publication cannot have comments
268
179
  ```
269
180
 
270
- For more information, see the [cucumber scenarios for feature
271
- specs](https://www.relishapp.com/rspec/rspec-rails/v/3-4/docs/feature-specs/feature-spec).
181
+ For an in-depth look at the RSpec DSL, including lots of examples,
182
+ read the official Cucumber documentation for [RSpec Core][].
272
183
 
273
- ## Mailer specs
184
+ [RSpec Core]: https://relishapp.com/rspec/rspec-core/docs
274
185
 
275
- By default Mailer specs reside in the `spec/mailers` folder. Adding the metadata
276
- `:type => :mailer` to any context makes its examples be treated as mailer specs.
186
+ ### Helpful Rails Matchers
277
187
 
278
- `ActionMailer::TestCase::Behavior` is mixed into your mailer specs.
188
+ In RSpec, assertions are called _expectations,_
189
+ and every expectation is built around a _matcher._
190
+ When you `expect(a).to eq(b)`, you’re using the `eq` matcher.
279
191
 
280
- ```ruby
281
- require "rails_helper"
192
+ In addition to [the matchers that come standard in RSpec][],
193
+ here are some extras that make it easier
194
+ to test the various parts of a Rails system:
282
195
 
283
- RSpec.describe Notifications, :type => :mailer do
284
- describe "notify" do
285
- let(:mail) { Notifications.signup }
196
+ | RSpec matcher | Delegates to | Available in | Notes |
197
+ | ------------------------ | ------------------- | ------------------------------- | -------------------------------------------------------- |
198
+ | [`be_a_new`][] | | all | primarily intended for controller specs |
199
+ | [`render_template`][] | `assert_template` | request / controller / view | use with `expect(response).to` |
200
+ | [`redirect_to`][] | `assert_redirect` | request / controller | use with `expect(response).to` |
201
+ | [`route_to`] | `assert_recognizes` | routing / controller | use with `expect(...).to route_to` |
202
+ | [`be_routable`] | | routing / controller | use with `expect(...).not_to be_routable` |
203
+ | [`have_http_status`][] | | request / controller / feature | |
204
+ | [`match_array`][] | | all | for comparing arrays of ActiveRecord objects |
205
+ | [`have_been_enqueued`][] | | all | requires config: `ActiveJob::Base.queue_adapter = :test` |
206
+ | [`have_enqueued_job`][] | | all | requires config: `ActiveJob::Base.queue_adapter = :test` |
286
207
 
287
- it "renders the headers" do
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
208
+ Follow the links above for examples of how each matcher is used.
292
209
 
293
- it "renders the body" do
294
- expect(mail.body.encoded).to match("Hi")
295
- end
296
- end
297
- end
298
- ```
210
+ [the matchers that come standard in RSpec]: https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers
211
+ [`be_a_new`]: https://relishapp.com/rspec/rspec-rails/docs/matchers/be-a-new-matcher
212
+ [`render_template`]: https://relishapp.com/rspec/rspec-rails/docs/matchers/render-template-matcher
213
+ [`redirect_to`]: https://relishapp.com/rspec/rspec-rails/docs/matchers/redirect-to-matcher
214
+ [`route_to`]: https://relishapp.com/rspec/rspec-rails/docs/routing-specs/route-to-matcher
215
+ [`be_routable`]: https://relishapp.com/rspec/rspec-rails/docs/routing-specs/be-routable-matcher
216
+ [`have_http_status`]: https://relishapp.com/rspec/rspec-rails/docs/matchers/have-http-status-matcher
217
+ [`match_array`]: https://relishapp.com/rspec/rspec-rails/docs/matchers/activerecord-relation-match-array
218
+ [`have_been_enqueued`]: https://relishapp.com/rspec/rspec-rails/docs/matchers/have-been-enqueued-matcher
219
+ [`have_enqueued_job`]: https://relishapp.com/rspec/rspec-rails/docs/matchers/have-enqueued-job-matcher
299
220
 
300
- For more information, see the [cucumber scenarios for mailer specs
301
- ](https://relishapp.com/rspec/rspec-rails/v/3-4/docs/mailer-specs).
221
+ ### What else does RSpec Rails add?
302
222
 
303
- ## Job specs
223
+ For a comprehensive look at RSpec Rails’ features,
224
+ read the [official Cucumber documentation][].
304
225
 
305
- Tagging a context with the metadata `:type => :job` treats its examples as job
306
- specs. Typically these specs will live in `spec/jobs`.
226
+ [official Cucumber documentation]: https://relishapp.com/rspec/rspec-rails/docs
307
227
 
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
- ```
228
+ ## What tests should I write?
321
229
 
322
- For more information, see the [cucumber scenarios for job specs
323
- ](https://relishapp.com/rspec/rspec-rails/docs/job-specs).
230
+ RSpec Rails defines ten different _types_ of specs
231
+ for testing different parts of a typical Rails application.
232
+ Each one inherits from one of Rails’ built-in `TestCase` classes,
233
+ meaning the helper methods provided by default in Rails tests
234
+ are available in RSpec, as well.
324
235
 
325
- ## View specs
236
+ | Spec type | Corresponding Rails test class |
237
+ | -------------- | -------------------------------- |
238
+ | [model][] | |
239
+ | [controller][] | [`ActionController::TestCase`][] |
240
+ | [mailer][] | `ActionMailer::TestCase` |
241
+ | [job][] | |
242
+ | [view][] | `ActionView::TestCase` |
243
+ | [routing][] | |
244
+ | [helper][] | `ActionView::TestCase` |
245
+ | [request][] | [`ActionDispatch::IntegrationTest`][] |
246
+ | [feature][] | |
247
+ | [system][] | [`ActionDispatch::SystemTestCase`][] |
326
248
 
327
- View specs default to residing in the `spec/views` folder. Tagging any context
328
- with the metadata `:type => :view` treats its examples as view specs.
249
+ Follow the links above to see examples of each spec type,
250
+ or for official Rails API documentation on the given `TestCase` class.
329
251
 
330
- View specs mix in `ActionView::TestCase::Behavior`.
252
+ > **Note: This is not a checklist.**
253
+ >
254
+ > Ask a hundred developers how to test an application,
255
+ > and you’ll get a hundred different answers.
256
+ >
257
+ > RSpec Rails provides thoughtfully selected features
258
+ > to encourage good testing practices, but there’s no “right” way to do it.
259
+ > Ultimately, it’s up to you to decide how your test suite will be composed.
260
+
261
+ When creating a spec file,
262
+ assign it a type in the top-level `describe` block, like so:
331
263
 
332
264
  ```ruby
333
- require 'rails_helper'
265
+ # spec/models/user_spec.rb
334
266
 
335
- RSpec.describe "events/index", :type => :view do
336
- it "renders _event partial for each event" do
337
- assign(:events, [double(Event), double(Event)])
338
- render
339
- expect(view).to render_template(:partial => "_event", :count => 2)
340
- end
341
- end
342
-
343
- RSpec.describe "events/show", :type => :view do
344
- it "displays the event location" do
345
- assign(:event, Event.new(:location => "Chicago"))
346
- render
347
- expect(rendered).to include("Chicago")
348
- end
349
- end
267
+ RSpec.describe User, type: :model do
268
+ ...
350
269
  ```
351
270
 
352
- View specs infer the controller name and path from the path to the view
353
- template. e.g. if the template is `events/index.html.erb` then:
271
+ [request]: https://relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec
272
+ [feature]: https://www.relishapp.com/rspec/rspec-rails/docs/feature-specs/feature-spec
273
+ [system]: https://relishapp.com/rspec/rspec-rails/docs/system-specs/system-spec
274
+ [model]: https://www.relishapp.com/rspec/rspec-rails/docs/model-specs
275
+ [controller]: https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs
276
+ [mailer]: https://relishapp.com/rspec/rspec-rails/docs/mailer-specs
277
+ [job]: https://relishapp.com/rspec/rspec-rails/docs/job-specs/job-spec
278
+ [view]: https://www.relishapp.com/rspec/rspec-rails/docs/view-specs/view-spec
279
+ [routing]: https://www.relishapp.com/rspec/rspec-rails/docs/routing-specs
280
+ [helper]: https://www.relishapp.com/rspec/rspec-rails/docs/helper-specs/helper-spec
281
+ [`ActionDispatch::IntegrationTest`]: https://api.rubyonrails.org/classes/ActionDispatch/IntegrationTest.html
282
+ [`ActionDispatch::SystemTestCase`]: https://api.rubyonrails.org/classes/ActionDispatch/SystemTestCase.html
283
+ [`ActionController::TestCase`]: https://api.rubyonrails.org/classes/ActionController/TestCase.html
284
+ [in the appropriate folder]: https://relishapp.com/rspec/rspec-rails/docs/directory-structure
354
285
 
355
- ```ruby
356
- controller.controller_path == "events"
357
- controller.request.path_parameters[:controller] == "events"
358
- ```
286
+ ### System specs, feature specs, request specs–what’s the difference?
359
287
 
360
- This means that most of the time you don't need to set these values. When
361
- spec'ing a partial that is included across different controllers, you _may_
362
- need to override these values before rendering the view.
288
+ RSpec Rails provides some end-to-end (entire application) testing capability
289
+ to specify the interaction with the client.
363
290
 
364
- To provide a layout for the render, you'll need to specify _both_ the template
365
- and the layout explicitly. For example:
291
+ #### System specs
366
292
 
367
- ```ruby
368
- render :template => "events/show", :layout => "layouts/application"
369
- ```
293
+ Also called **acceptance tests**, **browser tests**, or **end-to-end tests**,
294
+ system specs test the application from the perspective of a _human client._
295
+ The test code walks through a user’s browser interactions,
370
296
 
371
- ### `assign(key, val)`
297
+ * `visit '/login'`
298
+ * `fill_in 'Name', with: 'jdoe'`
372
299
 
373
- Use this to assign values to instance variables in the view:
300
+ and the expectations revolve around page content.
374
301
 
375
- ```ruby
376
- assign(:widget, Widget.new)
377
- render
378
- ```
302
+ * `expect(page).to have_text('Welcome')`
379
303
 
380
- The code above assigns `Widget.new` to the `@widget` variable in the view, and
381
- then renders the view.
304
+ Because system specs are a wrapper around Rails’ built-in `SystemTestCase`,
305
+ they’re only available on Rails 5.1+.
306
+ (Feature specs serve the same purpose, but without this dependency.)
382
307
 
383
- Note that because view specs mix in `ActionView::TestCase` behavior, any
384
- instance variables you set will be transparently propagated into your views
385
- (similar to how instance variables you set in controller actions are made
386
- available in views). For example:
308
+ #### Feature specs
387
309
 
388
- ```ruby
389
- @widget = Widget.new
390
- render # @widget is available inside the view
391
- ```
310
+ Before Rails introduced system testing facilities,
311
+ feature specs were the only spec type for end-to-end testing.
312
+ While the RSpec team now [officially recommends system specs][] instead,
313
+ feature specs are still fully supported, look basically identical,
314
+ and work on older versions of Rails.
392
315
 
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.
316
+ On the other hand, feature specs require non-trivial configuration
317
+ to get some important features working,
318
+ like JavaScript testing or making sure each test runs with a fresh DB state.
319
+ With system specs, this configuration is provided out-of-the-box.
396
320
 
397
- #### Upgrade note
321
+ Like system specs, feature specs require the [Capybara][] gem.
322
+ Rails 5.1+ includes it by default as part of system tests,
323
+ but if you don’t have the luxury of upgrading,
324
+ be sure to add it to the `:test` group of your `Gemfile` first:
398
325
 
399
326
  ```ruby
400
- # rspec-rails-1.x
401
- assigns[key] = value
402
-
403
- # rspec-rails-2.x+
404
- assign(key, value)
405
- ```
406
-
407
- ### `rendered`
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/
414
- ```
415
-
416
- #### Upgrade note
417
-
418
- ```ruby
419
- # rspec-rails-1.x
420
- render
421
- response.should xxx
422
-
423
- # rspec-rails-2.x+
424
- render
425
- rendered.should xxx
426
-
427
- # rspec-rails-2.x+ with expect syntax
428
- render
429
- expect(rendered).to xxx
430
- ```
431
-
432
- ## Routing specs
433
-
434
- Routing specs default to residing in the `spec/routing` folder. Tagging any
435
- context with the metadata `:type => :routing` treats its examples as routing
436
- specs.
437
-
438
- ```ruby
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
449
-
450
- it "does not expose a list of profiles" do
451
- expect(:get => "/profiles").not_to be_routable
452
- end
327
+ group :test do
328
+ gem "capybara"
453
329
  end
454
330
  ```
455
331
 
456
- ### Upgrade note
457
-
458
- `route_for` from rspec-rails-1.x is gone. Use `route_to` and `be_routable`
459
- instead.
460
-
461
- ## Helper specs
462
-
463
- Helper specs default to residing in the `spec/helpers` folder. Tagging any
464
- context with the metadata `:type => :helper` treats its examples as helper
465
- specs.
466
-
467
- Helper specs mix in ActionView::TestCase::Behavior. A `helper` object is
468
- provided which mixes in the helper module being spec'd, along with
469
- `ApplicationHelper` (if present).
470
-
471
- ```ruby
472
- require 'rails_helper'
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
483
- end
484
- ```
485
-
486
- ## Matchers
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?`
332
+ [officially recommends system specs]: https://rspec.info/blog/2017/10/rspec-3-7-has-been-released/#rails-actiondispatchsystemtest-integration-system-specs
333
+ [Capybara]: https://github.com/teamcapybara/capybara
501
334
 
502
- ### `render_template`
335
+ #### Request specs
503
336
 
504
- - Delegates to Rails' `assert_template`
505
- - Available in request, controller, and view specs
337
+ Request specs are for testing the application
338
+ from the perspective of a _machine client._
339
+ They begin with an HTTP request and end with the HTTP response,
340
+ so they’re faster than feature specs,
341
+ but do not examine your app’s UI or JavaScript.
506
342
 
507
- In request and controller specs, apply to the `response` object:
343
+ Request specs provide a high-level alternative to controller specs.
344
+ In fact, as of RSpec 3.5, both the Rails and RSpec teams
345
+ [discourage directly testing controllers][]
346
+ in favor of functional tests like request specs.
508
347
 
509
- ```ruby
510
- expect(response).to render_template("new")
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:
348
+ When writing them, try to answer the question,
349
+ “For a given HTTP request (verb + path + parameters),
350
+ what HTTP response should the application return?”
560
351
 
561
- ```ruby
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:
352
+ [discourage directly testing controllers]: https://rspec.info/blog/2016/07/rspec-3-5-has-been-released/#rails-support-for-rails-5
567
353
 
568
- ```ruby
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`.
354
+ ## Contributing
580
355
 
581
- ### Customizing `rake` tasks
356
+ - [Build details](BUILD_DETAIL.md)
357
+ - [Code of Conduct](CODE_OF_CONDUCT.md)
358
+ - [Detailed contributing guide](CONTRIBUTING.md)
582
359
 
583
- If you want to customize the behavior of `rake spec`, you may [define your own
584
- task in the `Rakefile` for your
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:
360
+ Once you’ve cloned the repo and [set up the environment](DEVELOPMENT.md),
361
+ you can run the specs and Cucumber features, or submit a pull request.
587
362
 
588
- ```ruby
589
- task("spec").clear
590
- ```
363
+ ## See Also
591
364
 
365
+ ### RSpec base libraries
592
366
 
593
- ## Also see
367
+ * <https://github.com/rspec/rspec>
368
+ * <https://github.com/rspec/rspec-core>
369
+ * <https://github.com/rspec/rspec-expectations>
370
+ * <https://github.com/rspec/rspec-mocks>
594
371
 
595
- * [https://github.com/rspec/rspec](https://github.com/rspec/rspec)
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)
372
+ ### Recommended third-party extensions
599
373
 
600
- ## Feature Requests & Bugs
374
+ * [FactoryBot](https://github.com/thoughtbot/factory_bot)
375
+ * [Capybara](https://github.com/teamcapybara/capybara)
376
+ (Included by default in Rails 5.1+.
377
+ Note that [additional configuration is required][] to use the Capybara DSL
378
+ anywhere other than system specs and feature specs.)
601
379
 
602
- See <http://github.com/rspec/rspec-rails/issues>
380
+ [additional configuration is required]: https://rubydoc.info/gems/rspec-rails/file/Capybara.md