rspec-rails 3.6.0 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data/Capybara.md +5 -54
  4. data/Changelog.md +391 -68
  5. data/README.md +280 -527
  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 +24 -7
  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 +5 -5
  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 +2 -2
  15. data/lib/generators/rspec/install/install_generator.rb +23 -6
  16. data/lib/generators/rspec/install/templates/spec/rails_helper.rb +25 -13
  17. data/lib/generators/rspec/integration/integration_generator.rb +13 -6
  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 +5 -4
  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 +8 -7
  26. data/lib/generators/rspec/model/templates/fixtures.yml +1 -1
  27. data/lib/generators/rspec/request/request_generator.rb +10 -3
  28. data/lib/generators/rspec/{integration → request}/templates/request_spec.rb +1 -1
  29. data/lib/generators/rspec/scaffold/scaffold_generator.rb +50 -26
  30. data/lib/generators/rspec/scaffold/templates/api_controller_spec.rb +15 -51
  31. data/lib/generators/rspec/scaffold/templates/api_request_spec.rb +131 -0
  32. data/lib/generators/rspec/scaffold/templates/controller_spec.rb +32 -65
  33. data/lib/generators/rspec/scaffold/templates/edit_spec.rb +9 -9
  34. data/lib/generators/rspec/scaffold/templates/index_spec.rb +3 -2
  35. data/lib/generators/rspec/scaffold/templates/new_spec.rb +1 -5
  36. data/lib/generators/rspec/scaffold/templates/request_spec.rb +153 -0
  37. data/lib/generators/rspec/scaffold/templates/routing_spec.rb +10 -13
  38. data/lib/generators/rspec/scaffold/templates/show_spec.rb +2 -2
  39. data/lib/generators/rspec/system/system_generator.rb +24 -0
  40. data/lib/generators/rspec/system/templates/system_spec.rb +9 -0
  41. data/lib/generators/rspec/view/view_generator.rb +4 -4
  42. data/lib/generators/rspec.rb +16 -5
  43. data/lib/rspec/rails/active_record.rb +1 -1
  44. data/lib/rspec/rails/adapters.rb +21 -76
  45. data/lib/rspec/rails/configuration.rb +86 -35
  46. data/lib/rspec/rails/example/channel_example_group.rb +93 -0
  47. data/lib/rspec/rails/example/controller_example_group.rb +5 -4
  48. data/lib/rspec/rails/example/feature_example_group.rb +6 -26
  49. data/lib/rspec/rails/example/helper_example_group.rb +2 -9
  50. data/lib/rspec/rails/example/mailbox_example_group.rb +80 -0
  51. data/lib/rspec/rails/example/mailer_example_group.rb +2 -2
  52. data/lib/rspec/rails/example/rails_example_group.rb +2 -1
  53. data/lib/rspec/rails/example/request_example_group.rb +1 -4
  54. data/lib/rspec/rails/example/system_example_group.rb +126 -0
  55. data/lib/rspec/rails/example/view_example_group.rb +47 -28
  56. data/lib/rspec/rails/example.rb +3 -0
  57. data/lib/rspec/rails/extensions/active_record/proxy.rb +5 -10
  58. data/lib/rspec/rails/feature_check.rb +16 -29
  59. data/lib/rspec/rails/file_fixture_support.rb +9 -11
  60. data/lib/rspec/rails/fixture_file_upload_support.rb +39 -0
  61. data/lib/rspec/rails/fixture_support.rb +35 -32
  62. data/lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb +170 -0
  63. data/lib/rspec/rails/matchers/action_cable/have_streams.rb +58 -0
  64. data/lib/rspec/rails/matchers/action_cable.rb +65 -0
  65. data/lib/rspec/rails/matchers/action_mailbox.rb +73 -0
  66. data/lib/rspec/rails/matchers/active_job.rb +209 -25
  67. data/lib/rspec/rails/matchers/base_matcher.rb +179 -0
  68. data/lib/rspec/rails/matchers/be_a_new.rb +6 -5
  69. data/lib/rspec/rails/matchers/be_new_record.rb +1 -1
  70. data/lib/rspec/rails/matchers/be_valid.rb +1 -1
  71. data/lib/rspec/rails/matchers/have_enqueued_mail.rb +227 -0
  72. data/lib/rspec/rails/matchers/have_http_status.rb +39 -18
  73. data/lib/rspec/rails/matchers/have_rendered.rb +2 -1
  74. data/lib/rspec/rails/matchers/redirect_to.rb +1 -1
  75. data/lib/rspec/rails/matchers/relation_match_array.rb +1 -1
  76. data/lib/rspec/rails/matchers/routing_matchers.rb +13 -13
  77. data/lib/rspec/rails/matchers.rb +11 -0
  78. data/lib/rspec/rails/tasks/rspec.rake +7 -17
  79. data/lib/rspec/rails/vendor/capybara.rb +10 -13
  80. data/lib/rspec/rails/version.rb +1 -1
  81. data/lib/rspec/rails/view_assigns.rb +0 -18
  82. data/lib/rspec/rails/view_path_builder.rb +2 -2
  83. data/lib/rspec/rails/view_rendering.rb +10 -17
  84. data/lib/rspec/rails.rb +1 -0
  85. data/lib/rspec-rails.rb +10 -13
  86. data.tar.gz.sig +0 -0
  87. metadata +58 -35
  88. metadata.gz.sig +0 -0
  89. data/lib/generators/rspec/observer/observer_generator.rb +0 -13
data/README.md CHANGED
@@ -1,629 +1,382 @@
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.0.
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` 6.x][]** for Rails 6.1 or 7.x.
13
+ * **[`rspec-rails` 5.x][]** for Rails 5.2 or 6.x.
14
+ * **[`rspec-rails` 4.x][]** for Rails from 5.x or 6.x.
15
+ * **[`rspec-rails` 3.x][]** for Rails earlier than 5.0.
16
+ * **[`rspec-rails` 1.x][]** for Rails 2.x.
17
+
18
+ [Build Status]: https://secure.travis-ci.org/rspec/rspec-rails.svg?branch=main
19
+ [travis-ci]: https://travis-ci.org/rspec/rspec-rails
20
+ [Code Climate]: https://codeclimate.com/github/rspec/rspec-rails.svg
21
+ [code-climate]: https://codeclimate.com/github/rspec/rspec-rails
22
+ [Gem Version]: https://badge.fury.io/rb/rspec-rails.svg
23
+ [gem-version]: https://badge.fury.io/rb/rspec-rails
24
+ [RSpec]: https://rspec.info/
25
+ [Ruby on Rails]: https://rubyonrails.org/
26
+ [`rspec-rails` 1.x]: https://github.com/dchelimsky/rspec-rails
27
+ [`rspec-rails` 3.x]: https://github.com/rspec/rspec-rails/tree/3-9-maintenance
28
+ [`rspec-rails` 4.x]: https://github.com/rspec/rspec-rails/tree/4-1-maintenance
29
+ [`rspec-rails` 5.x]: https://github.com/rspec/rspec-rails/tree/5-1-maintenance
30
+ [`rspec-rails` 6.x]: https://github.com/rspec/rspec-rails/tree/6-0-maintenance
31
+ [RSpec Rails new versioning strategy]: https://github.com/rspec/rspec-rails/blob/main/rfcs/versioning-strategy.md
6
32
 
7
33
  ## Installation
8
34
 
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.5'
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`:
20
-
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
- ```
26
-
27
- Download and install by running:
28
-
29
- ```
30
- bundle install
31
- ```
32
-
33
- Initialize the `spec/` directory (where specs will reside) with:
34
-
35
- ```
36
- rails generate rspec:install
37
- ```
38
-
39
- This adds the following files which are used for configuration:
40
-
41
- - `.rspec`
42
- - `spec/spec_helper.rb`
43
- - `spec/rails_helper.rb`
44
-
45
- Check the comments in each file for more information.
35
+ **IMPORTANT** This README / branch refers to the current development build.
36
+ See the [`6-0-maintenance` branch on Github](https://github.com/rspec/rspec-rails/tree/6-0-maintenance) if you want or require the latest stable release.
46
37
 
47
- Use the `rspec` command to run your specs:
38
+ 1. Add `rspec-rails` to **both** the `:development` and `:test` groups
39
+ of your app’s `Gemfile`:
48
40
 
49
- ```
50
- bundle exec rspec
51
- ```
41
+ ```ruby
42
+ # Run against this stable release
43
+ group :development, :test do
44
+ gem 'rspec-rails', '~> 6.0.0'
45
+ end
52
46
 
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).
47
+ # Or, run against the main branch
48
+ # (requires main-branch versions of all related RSpec libraries)
49
+ group :development, :test do
50
+ %w[rspec-core rspec-expectations rspec-mocks rspec-rails rspec-support].each do |lib|
51
+ gem lib, git: "https://github.com/rspec/#{lib}.git", branch: 'main'
52
+ end
53
+ end
54
+ ```
56
55
 
57
- To run only a subset of these specs use the following command:
56
+ (Adding it to the `:development` group is not strictly necessary,
57
+ but without it, generators and rake tasks must be preceded by `RAILS_ENV=test`.)
58
58
 
59
- ```
60
- # Run only model specs
61
- bundle exec rspec spec/models
59
+ 2. Then, in your project directory:
62
60
 
63
- # Run only specs for AccountsController
64
- bundle exec rspec spec/controllers/accounts_controller_spec.rb
61
+ ```sh
62
+ # Download and install
63
+ $ bundle install
65
64
 
66
- # Run only spec on line 8 of AccountsController
67
- bundle exec rspec spec/controllers/accounts_controller_spec.rb:8
68
- ```
65
+ # Generate boilerplate configuration files
66
+ # (check the comments in each generated file for more information)
67
+ $ rails generate rspec:install
68
+ create .rspec
69
+ create spec
70
+ create spec/spec_helper.rb
71
+ create spec/rails_helper.rb
72
+ ```
69
73
 
70
- Specs can also be run via `rake spec`, though this command may be slower to
71
- start than the `rspec` command.
74
+ ## Upgrading
72
75
 
73
- In Rails 4, you may want to create a binstub for the `rspec` command so it can
74
- be run via `bin/rspec`:
76
+ If your project is already using an older version of `rspec-rails`,
77
+ upgrade to the latest version with:
75
78
 
79
+ ```sh
80
+ $ bundle update rspec-rails
76
81
  ```
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
-
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.
99
-
100
- Please see the [RSpec Rails Upgrade
101
- docs](https://www.relishapp.com/rspec/rspec-rails/docs/upgrade) for full
102
- details.
103
-
104
- **NOTE:** Generators run in RSpec 3.x will now require `rails_helper` instead
105
- of `spec_helper`.
106
-
107
- ### Generators
108
-
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.
112
-
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
82
 
83
+ RSpec follows [semantic versioning](https://semver.org/),
84
+ which means that “major version” upgrades (_e.g.,_ 2.x → 3.x)
85
+ come with **breaking changes**.
86
+ If you’re upgrading from version 2.x or below,
87
+ read the [`rspec-rails` upgrade notes][] to find out what to watch out for.
135
88
 
136
- ## Model Specs
89
+ Be sure to check the general [RSpec upgrade notes][] as well.
137
90
 
138
- Use model specs to describe behavior of models (usually ActiveRecord-based) in
139
- the application.
91
+ [`rspec-rails` upgrade notes]: https://www.relishapp.com/rspec/rspec-rails/docs/upgrade
92
+ [RSpec upgrade notes]: https://relishapp.com/rspec/docs/upgrade
140
93
 
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.
94
+ ## Usage
144
95
 
145
- For example:
96
+ ### Creating boilerplate specs with `rails generate`
146
97
 
147
- ```ruby
148
- require "rails_helper"
98
+ ```sh
99
+ # RSpec hooks into built-in generators
100
+ $ rails generate model user
101
+ invoke active_record
102
+ create db/migrate/20181017040312_create_users.rb
103
+ create app/models/user.rb
104
+ invoke rspec
105
+ create spec/models/user_spec.rb
149
106
 
150
- RSpec.describe User, :type => :model do
151
- it "orders by last name" do
152
- lindeman = User.create!(first_name: "Andy", last_name: "Lindeman")
153
- chelimsky = User.create!(first_name: "David", last_name: "Chelimsky")
107
+ # RSpec also provides its own spec file generators
108
+ $ rails generate rspec:model user
109
+ create spec/models/user_spec.rb
154
110
 
155
- expect(User.ordered_by_last_name).to eq([chelimsky, lindeman])
156
- end
157
- end
111
+ # List all RSpec generators
112
+ $ rails generate --help | grep rspec
158
113
  ```
159
114
 
160
- For more information, see [cucumber scenarios for model
161
- specs](https://www.relishapp.com/rspec/rspec-rails/docs/model-specs).
162
-
163
- ## Controller Specs
115
+ ### Running specs
164
116
 
165
- Use controller specs to describe behavior of Rails controllers.
117
+ ```sh
118
+ # Default: Run all spec files (i.e., those matching spec/**/*_spec.rb)
119
+ $ bundle exec rspec
166
120
 
167
- Controller specs default to residing in the `spec/controllers` folder. Tagging
168
- any context with the metadata `:type => :controller` treats its examples as
169
- controller specs.
121
+ # Run all spec files in a single directory (recursively)
122
+ $ bundle exec rspec spec/models
170
123
 
171
- For example:
172
-
173
- ```ruby
174
- require "rails_helper"
175
-
176
- RSpec.describe PostsController, :type => :controller do
177
- describe "GET #index" do
178
- it "responds successfully with an HTTP 200 status code" do
179
- get :index
180
- expect(response).to be_success
181
- expect(response).to have_http_status(200)
182
- end
183
-
184
- it "renders the index template" do
185
- get :index
186
- expect(response).to render_template("index")
187
- end
124
+ # Run a single spec file
125
+ $ bundle exec rspec spec/controllers/accounts_controller_spec.rb
188
126
 
189
- it "loads all of the posts into @posts" do
190
- post1, post2 = Post.create!, Post.create!
191
- get :index
127
+ # Run a single example from a spec file (by line number)
128
+ $ bundle exec rspec spec/controllers/accounts_controller_spec.rb:8
192
129
 
193
- expect(assigns(:posts)).to match_array([post1, post2])
194
- end
195
- end
196
- end
130
+ # See all options for running specs
131
+ $ bundle exec rspec --help
197
132
  ```
198
133
 
199
- For more information, see [cucumber scenarios for controller
200
- specs](https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs).
201
-
202
- **Note:** To encourage more isolated testing, views are not rendered by default
203
- in controller specs. If you are verifying discrete view logic, use a [view
204
- spec](#view-specs). If you are verifying the behaviour of a controller and view
205
- together, consider a [request spec](#request-specs). You can use
206
- [render\_views](https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/render-views)
207
- if you must verify the rendered view contents within a controller spec, but
208
- this is not recommended.
209
-
210
- ## Request Specs
134
+ **Optional:** If `bundle exec rspec` is too verbose for you,
135
+ you can generate a binstub at `bin/rspec` and use that instead:
211
136
 
212
- Use request specs to specify one or more request/response cycles from end to
213
- end using a black box approach.
137
+ ```sh
138
+ $ bundle binstubs rspec-core
139
+ ```
214
140
 
215
- Request specs default to residing in the `spec/requests`, `spec/api`, and
216
- `spec/integration` directories. Tagging any context with the metadata `:type =>
217
- :request` treats its examples as request specs.
141
+ ## RSpec DSL Basics (or, how do I write a spec?)
218
142
 
219
- Request specs mix in behavior from
220
- [ActionDispatch::Integration::Runner](http://api.rubyonrails.org/classes/ActionDispatch/Integration/Runner.html),
221
- which is the basis for [Rails' integration
222
- tests](http://guides.rubyonrails.org/testing.html#integration-testing).
143
+ In RSpec, application behavior is described
144
+ **first in (almost) plain English, then again in test code**, like so:
223
145
 
224
146
  ```ruby
225
- require 'rails_helper'
226
-
227
- RSpec.describe "home page", :type => :request do
228
- it "displays the user's username after successful login" do
229
- user = User.create!(:username => "jdoe", :password => "secret")
230
- get "/login"
231
- assert_select "form.login" do
232
- assert_select "input[name=?]", "username"
233
- assert_select "input[name=?]", "password"
234
- assert_select "input[type=?]", "submit"
147
+ RSpec.describe 'Post' do #
148
+ context 'before publication' do # (almost) plain English
149
+ it 'cannot have comments' do #
150
+ expect { Post.create.comments.create! }.to raise_error(ActiveRecord::RecordInvalid) # test code
235
151
  end
236
-
237
- post "/login", :username => "jdoe", :password => "secret"
238
- assert_select ".header .username", :text => "jdoe"
239
152
  end
240
153
  end
241
154
  ```
242
155
 
243
- The above example uses only standard Rails and RSpec APIs, but many
244
- RSpec/Rails users like to use extension libraries like
245
- [FactoryGirl](https://github.com/thoughtbot/factory_girl) and
246
- [Capybara](https://github.com/jnicklas/capybara):
247
-
248
- ```ruby
249
- require 'rails_helper'
156
+ Running `rspec` will execute this test code,
157
+ and then use the plain-English descriptions
158
+ to generate a report of where the application
159
+ conforms to (or fails to meet) the spec:
250
160
 
251
- RSpec.describe "home page", :type => :request do
252
- it "displays the user's username after successful login" do
253
- user = FactoryGirl.create(:user, :username => "jdoe", :password => "secret")
254
- visit "/login"
255
- fill_in "Username", :with => "jdoe"
256
- fill_in "Password", :with => "secret"
257
- click_button "Log in"
258
-
259
- expect(page).to have_selector(".header .username", :text => "jdoe")
260
- end
261
- end
262
161
  ```
162
+ $ rspec --format documentation spec/models/post_spec.rb
263
163
 
264
- FactoryGirl decouples this example from changes to validation requirements,
265
- which can be encoded into the underlying factory definition without requiring
266
- changes to this example.
267
-
268
- Among other benefits, Capybara binds the form post to the generated HTML, which
269
- means we don't need to specify them separately. Note that Capybara's DSL as
270
- shown is, by default, only available in specs in the spec/features directory.
271
- For more information, see the [Capybara integration
272
- docs](http://rubydoc.info/gems/rspec-rails/file/Capybara.md).
273
-
274
- There are several other Ruby libs that implement the factory pattern or provide
275
- a DSL for request specs (a.k.a. acceptance or integration specs), but
276
- FactoryGirl and Capybara seem to be the most widely used. Whether you choose
277
- these or other libs, we strongly recommend using something for each of these
278
- roles.
164
+ Post
165
+ before publication
166
+ cannot have comments
279
167
 
280
- ## Feature Specs
168
+ Failures:
281
169
 
282
- Feature specs test your application from the outside by simulating a browser.
283
- [`capybara`](https://github.com/jnicklas/capybara) is used to manage the
284
- simulated browser.
170
+ 1) Post before publication cannot have comments
171
+ Failure/Error: expect { Post.create.comments.create! }.to raise_error(ActiveRecord::RecordInvalid)
172
+ expected ActiveRecord::RecordInvalid but nothing was raised
173
+ # ./spec/models/post.rb:4:in `block (3 levels) in <top (required)>'
285
174
 
286
- Feature specs default to residing in the `spec/features` folder. Tagging any
287
- context with the metadata `:type => :feature` treats its examples as feature
288
- specs.
175
+ Finished in 0.00527 seconds (files took 0.29657 seconds to load)
176
+ 1 example, 1 failure
289
177
 
290
- Feature specs mix in functionality from the capybara gem, thus they require
291
- `capybara` to use. To use feature specs, add `capybara` to the `Gemfile`:
178
+ Failed examples:
292
179
 
293
- ```ruby
294
- gem "capybara"
180
+ rspec ./spec/models/post_spec.rb:3 # Post before publication cannot have comments
295
181
  ```
296
182
 
297
- For more information, see the [cucumber scenarios for feature
298
- specs](https://www.relishapp.com/rspec/rspec-rails/v/3-4/docs/feature-specs/feature-spec).
183
+ For an in-depth look at the RSpec DSL, including lots of examples,
184
+ read the official Cucumber documentation for [RSpec Core][].
299
185
 
300
- ## Mailer specs
186
+ [RSpec Core]: https://relishapp.com/rspec/rspec-core/docs
301
187
 
302
- By default Mailer specs reside in the `spec/mailers` folder. Adding the metadata
303
- `:type => :mailer` to any context makes its examples be treated as mailer specs.
188
+ ### Helpful Rails Matchers
304
189
 
305
- `ActionMailer::TestCase::Behavior` is mixed into your mailer specs.
190
+ In RSpec, assertions are called _expectations,_
191
+ and every expectation is built around a _matcher._
192
+ When you `expect(a).to eq(b)`, you’re using the `eq` matcher.
306
193
 
307
- ```ruby
308
- require "rails_helper"
194
+ In addition to [the matchers that come standard in RSpec][],
195
+ here are some extras that make it easier
196
+ to test the various parts of a Rails system:
309
197
 
310
- RSpec.describe Notifications, :type => :mailer do
311
- describe "notify" do
312
- let(:mail) { Notifications.signup }
198
+ | RSpec matcher | Delegates to | Available in | Notes |
199
+ | ------------------------ | ------------------- | ------------------------------- | -------------------------------------------------------- |
200
+ | [`be_a_new`][] | | all | primarily intended for controller specs |
201
+ | [`render_template`][] | `assert_template` | request / controller / view | use with `expect(response).to` |
202
+ | [`redirect_to`][] | `assert_redirect` | request / controller | use with `expect(response).to` |
203
+ | [`route_to`] | `assert_recognizes` | routing / controller | use with `expect(...).to route_to` |
204
+ | [`be_routable`] | | routing / controller | use with `expect(...).not_to be_routable` |
205
+ | [`have_http_status`][] | | request / controller / feature | |
206
+ | [`match_array`][] | | all | for comparing arrays of ActiveRecord objects |
207
+ | [`have_been_enqueued`][] | | all | requires config: `ActiveJob::Base.queue_adapter = :test` |
208
+ | [`have_enqueued_job`][] | | all | requires config: `ActiveJob::Base.queue_adapter = :test` |
313
209
 
314
- it "renders the headers" do
315
- expect(mail.subject).to eq("Signup")
316
- expect(mail.to).to eq(["to@example.org"])
317
- expect(mail.from).to eq(["from@example.com"])
318
- end
210
+ Follow the links above for examples of how each matcher is used.
319
211
 
320
- it "renders the body" do
321
- expect(mail.body.encoded).to match("Hi")
322
- end
323
- end
324
- end
325
- ```
212
+ [the matchers that come standard in RSpec]: https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers
213
+ [`be_a_new`]: https://relishapp.com/rspec/rspec-rails/docs/matchers/be-a-new-matcher
214
+ [`render_template`]: https://relishapp.com/rspec/rspec-rails/docs/matchers/render-template-matcher
215
+ [`redirect_to`]: https://relishapp.com/rspec/rspec-rails/docs/matchers/redirect-to-matcher
216
+ [`route_to`]: https://relishapp.com/rspec/rspec-rails/docs/routing-specs/route-to-matcher
217
+ [`be_routable`]: https://relishapp.com/rspec/rspec-rails/docs/routing-specs/be-routable-matcher
218
+ [`have_http_status`]: https://relishapp.com/rspec/rspec-rails/docs/matchers/have-http-status-matcher
219
+ [`match_array`]: https://relishapp.com/rspec/rspec-rails/docs/matchers/activerecord-relation-match-array
220
+ [`have_been_enqueued`]: https://relishapp.com/rspec/rspec-rails/docs/matchers/have-been-enqueued-matcher
221
+ [`have_enqueued_job`]: https://relishapp.com/rspec/rspec-rails/docs/matchers/have-enqueued-job-matcher
326
222
 
327
- For more information, see the [cucumber scenarios for mailer specs
328
- ](https://relishapp.com/rspec/rspec-rails/v/3-4/docs/mailer-specs).
223
+ ### What else does RSpec Rails add?
329
224
 
330
- ## Job specs
225
+ For a comprehensive look at RSpec Rails’ features,
226
+ read the [official Cucumber documentation][].
331
227
 
332
- Tagging a context with the metadata `:type => :job` treats its examples as job
333
- specs. Typically these specs will live in `spec/jobs`.
228
+ [official Cucumber documentation]: https://relishapp.com/rspec/rspec-rails/docs
334
229
 
335
- ```ruby
336
- require 'rails_helper'
337
-
338
- RSpec.describe UploadBackupsJob, :type => :job do
339
- describe "#perform_later" do
340
- it "uploads a backup" do
341
- ActiveJob::Base.queue_adapter = :test
342
- UploadBackupsJob.perform_later('backup')
343
- expect(UploadBackupsJob).to have_been_enqueued
344
- end
345
- end
346
- end
347
- ```
230
+ ## What tests should I write?
348
231
 
349
- For more information, see the [cucumber scenarios for job specs
350
- ](https://relishapp.com/rspec/rspec-rails/docs/job-specs).
232
+ RSpec Rails defines ten different _types_ of specs
233
+ for testing different parts of a typical Rails application.
234
+ Each one inherits from one of Rails’ built-in `TestCase` classes,
235
+ meaning the helper methods provided by default in Rails tests
236
+ are available in RSpec, as well.
351
237
 
352
- ## View specs
238
+ | Spec type | Corresponding Rails test class |
239
+ | -------------- | -------------------------------- |
240
+ | [model][] | |
241
+ | [controller][] | [`ActionController::TestCase`][] |
242
+ | [mailer][] | `ActionMailer::TestCase` |
243
+ | [job][] | |
244
+ | [view][] | `ActionView::TestCase` |
245
+ | [routing][] | |
246
+ | [helper][] | `ActionView::TestCase` |
247
+ | [request][] | [`ActionDispatch::IntegrationTest`][] |
248
+ | [feature][] | |
249
+ | [system][] | [`ActionDispatch::SystemTestCase`][] |
353
250
 
354
- View specs default to residing in the `spec/views` folder. Tagging any context
355
- with the metadata `:type => :view` treats its examples as view specs.
251
+ Follow the links above to see examples of each spec type,
252
+ or for official Rails API documentation on the given `TestCase` class.
356
253
 
357
- View specs mix in `ActionView::TestCase::Behavior`.
254
+ > **Note: This is not a checklist.**
255
+ >
256
+ > Ask a hundred developers how to test an application,
257
+ > and you’ll get a hundred different answers.
258
+ >
259
+ > RSpec Rails provides thoughtfully selected features
260
+ > to encourage good testing practices, but there’s no “right” way to do it.
261
+ > Ultimately, it’s up to you to decide how your test suite will be composed.
262
+
263
+ When creating a spec file,
264
+ assign it a type in the top-level `describe` block, like so:
358
265
 
359
266
  ```ruby
360
- require 'rails_helper'
361
-
362
- RSpec.describe "events/index", :type => :view do
363
- it "renders _event partial for each event" do
364
- assign(:events, [double(Event), double(Event)])
365
- render
366
- expect(view).to render_template(:partial => "_event", :count => 2)
367
- end
368
- end
267
+ # spec/models/user_spec.rb
369
268
 
370
- RSpec.describe "events/show", :type => :view do
371
- it "displays the event location" do
372
- assign(:event, Event.new(:location => "Chicago"))
373
- render
374
- expect(rendered).to include("Chicago")
375
- end
376
- end
269
+ RSpec.describe User, type: :model do
270
+ ...
377
271
  ```
378
272
 
379
- View specs infer the controller name and path from the path to the view
380
- template. e.g. if the template is `events/index.html.erb` then:
273
+ [request]: https://relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec
274
+ [feature]: https://www.relishapp.com/rspec/rspec-rails/docs/feature-specs/feature-spec
275
+ [system]: https://relishapp.com/rspec/rspec-rails/docs/system-specs/system-spec
276
+ [model]: https://www.relishapp.com/rspec/rspec-rails/docs/model-specs
277
+ [controller]: https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs
278
+ [mailer]: https://relishapp.com/rspec/rspec-rails/docs/mailer-specs
279
+ [job]: https://relishapp.com/rspec/rspec-rails/docs/job-specs/job-spec
280
+ [view]: https://www.relishapp.com/rspec/rspec-rails/docs/view-specs/view-spec
281
+ [routing]: https://www.relishapp.com/rspec/rspec-rails/docs/routing-specs
282
+ [helper]: https://www.relishapp.com/rspec/rspec-rails/docs/helper-specs/helper-spec
283
+ [`ActionDispatch::IntegrationTest`]: https://api.rubyonrails.org/classes/ActionDispatch/IntegrationTest.html
284
+ [`ActionDispatch::SystemTestCase`]: https://api.rubyonrails.org/classes/ActionDispatch/SystemTestCase.html
285
+ [`ActionController::TestCase`]: https://api.rubyonrails.org/classes/ActionController/TestCase.html
286
+ [in the appropriate folder]: https://relishapp.com/rspec/rspec-rails/docs/directory-structure
381
287
 
382
- ```ruby
383
- controller.controller_path == "events"
384
- controller.request.path_parameters[:controller] == "events"
385
- ```
386
-
387
- This means that most of the time you don't need to set these values. When
388
- spec'ing a partial that is included across different controllers, you _may_
389
- need to override these values before rendering the view.
390
-
391
- To provide a layout for the render, you'll need to specify _both_ the template
392
- and the layout explicitly. For example:
393
-
394
- ```ruby
395
- render :template => "events/show", :layout => "layouts/application"
396
- ```
397
-
398
- ### `assign(key, val)`
399
-
400
- Use this to assign values to instance variables in the view:
401
-
402
- ```ruby
403
- assign(:widget, Widget.new)
404
- render
405
- ```
406
-
407
- The code above assigns `Widget.new` to the `@widget` variable in the view, and
408
- then renders the view.
409
-
410
- Note that because view specs mix in `ActionView::TestCase` behavior, any
411
- instance variables you set will be transparently propagated into your views
412
- (similar to how instance variables you set in controller actions are made
413
- available in views). For example:
414
-
415
- ```ruby
416
- @widget = Widget.new
417
- render # @widget is available inside the view
418
- ```
419
-
420
- RSpec doesn't officially support this pattern, which only works as a
421
- side-effect of the inclusion of `ActionView::TestCase`. Be aware that it may be
422
- made unavailable in the future.
423
-
424
- #### Upgrade note
425
-
426
- ```ruby
427
- # rspec-rails-1.x
428
- assigns[key] = value
429
-
430
- # rspec-rails-2.x+
431
- assign(key, value)
432
- ```
288
+ ### System specs, feature specs, request specs–what’s the difference?
433
289
 
434
- ### `rendered`
290
+ RSpec Rails provides some end-to-end (entire application) testing capability
291
+ to specify the interaction with the client.
435
292
 
436
- This represents the rendered view.
293
+ #### System specs
437
294
 
438
- ```ruby
439
- render
440
- expect(rendered).to match /Some text expected to appear on the page/
441
- ```
295
+ Also called **acceptance tests**, **browser tests**, or **end-to-end tests**,
296
+ system specs test the application from the perspective of a _human client._
297
+ The test code walks through a user’s browser interactions,
442
298
 
443
- #### Upgrade note
299
+ * `visit '/login'`
300
+ * `fill_in 'Name', with: 'jdoe'`
444
301
 
445
- ```ruby
446
- # rspec-rails-1.x
447
- render
448
- response.should xxx
302
+ and the expectations revolve around page content.
449
303
 
450
- # rspec-rails-2.x+
451
- render
452
- rendered.should xxx
304
+ * `expect(page).to have_text('Welcome')`
453
305
 
454
- # rspec-rails-2.x+ with expect syntax
455
- render
456
- expect(rendered).to xxx
457
- ```
306
+ Because system specs are a wrapper around Rails’ built-in `SystemTestCase`,
307
+ they’re only available on Rails 5.1+.
308
+ (Feature specs serve the same purpose, but without this dependency.)
458
309
 
459
- ## Routing specs
310
+ #### Feature specs
460
311
 
461
- Routing specs default to residing in the `spec/routing` folder. Tagging any
462
- context with the metadata `:type => :routing` treats its examples as routing
463
- specs.
312
+ Before Rails introduced system testing facilities,
313
+ feature specs were the only spec type for end-to-end testing.
314
+ While the RSpec team now [officially recommends system specs][] instead,
315
+ feature specs are still fully supported, look basically identical,
316
+ and work on older versions of Rails.
464
317
 
465
- ```ruby
466
- require 'rails_helper'
467
-
468
- RSpec.describe "routing to profiles", :type => :routing do
469
- it "routes /profile/:username to profile#show for username" do
470
- expect(:get => "/profiles/jsmith").to route_to(
471
- :controller => "profiles",
472
- :action => "show",
473
- :username => "jsmith"
474
- )
475
- end
476
-
477
- it "does not expose a list of profiles" do
478
- expect(:get => "/profiles").not_to be_routable
479
- end
480
- end
481
- ```
318
+ On the other hand, feature specs require non-trivial configuration
319
+ to get some important features working,
320
+ like JavaScript testing or making sure each test runs with a fresh DB state.
321
+ With system specs, this configuration is provided out-of-the-box.
482
322
 
483
- ### Upgrade note
484
-
485
- `route_for` from rspec-rails-1.x is gone. Use `route_to` and `be_routable`
486
- instead.
487
-
488
- ## Helper specs
489
-
490
- Helper specs default to residing in the `spec/helpers` folder. Tagging any
491
- context with the metadata `:type => :helper` treats its examples as helper
492
- specs.
493
-
494
- Helper specs mix in ActionView::TestCase::Behavior. A `helper` object is
495
- provided which mixes in the helper module being spec'd, along with
496
- `ApplicationHelper` (if present).
323
+ Like system specs, feature specs require the [Capybara][] gem.
324
+ Rails 5.1+ includes it by default as part of system tests,
325
+ but if you don’t have the luxury of upgrading,
326
+ be sure to add it to the `:test` group of your `Gemfile` first:
497
327
 
498
328
  ```ruby
499
- require 'rails_helper'
500
-
501
- RSpec.describe EventsHelper, :type => :helper do
502
- describe "#link_to_event" do
503
- it "displays the title, and formatted date" do
504
- event = Event.new("Ruby Kaigi", Date.new(2010, 8, 27))
505
- # helper is an instance of ActionView::Base configured with the
506
- # EventsHelper and all of Rails' built-in helpers
507
- expect(helper.link_to_event).to match /Ruby Kaigi, 27 Aug, 2010/
508
- end
509
- end
329
+ group :test do
330
+ gem "capybara"
510
331
  end
511
332
  ```
512
333
 
513
- ## Matchers
514
-
515
- Several domain-specific matchers are provided to each of the example group
516
- types. Most simply delegate to their equivalent Rails' assertions.
517
-
518
- ### `be_a_new`
334
+ [officially recommends system specs]: https://rspec.info/blog/2017/10/rspec-3-7-has-been-released/#rails-actiondispatchsystemtest-integration-system-specs
335
+ [Capybara]: https://github.com/teamcapybara/capybara
519
336
 
520
- - Available in all specs
521
- - Primarily intended for controller specs
337
+ #### Request specs
522
338
 
523
- ```ruby
524
- expect(object).to be_a_new(Widget)
525
- ```
339
+ Request specs are for testing the application
340
+ from the perspective of a _machine client._
341
+ They begin with an HTTP request and end with the HTTP response,
342
+ so they’re faster than feature specs,
343
+ but do not examine your app’s UI or JavaScript.
526
344
 
527
- Passes if the object is a `Widget` and returns true for `new_record?`
345
+ Request specs provide a high-level alternative to controller specs.
346
+ In fact, as of RSpec 3.5, both the Rails and RSpec teams
347
+ [discourage directly testing controllers][]
348
+ in favor of functional tests like request specs.
528
349
 
529
- ### `render_template`
350
+ When writing them, try to answer the question,
351
+ “For a given HTTP request (verb + path + parameters),
352
+ what HTTP response should the application return?”
530
353
 
531
- - Delegates to Rails' `assert_template`
532
- - Available in request, controller, and view specs
354
+ [discourage directly testing controllers]: https://rspec.info/blog/2016/07/rspec-3-5-has-been-released/#rails-support-for-rails-5
533
355
 
534
- In request and controller specs, apply to the `response` object:
535
-
536
- ```ruby
537
- expect(response).to render_template("new")
538
- ```
539
-
540
- In view specs, apply to the `view` object:
541
-
542
- ```ruby
543
- expect(view).to render_template(:partial => "_form", :locals => { :widget => widget } )
544
- ```
545
-
546
- ### `redirect_to`
547
-
548
- - Delegates to `assert_redirect`
549
- - Available in request and controller specs
550
-
551
- ```ruby
552
- expect(response).to redirect_to(widgets_path)
553
- ```
554
-
555
- ### `route_to`
556
-
557
- - Delegates to Rails' `assert_routing`
558
- - Available in routing and controller specs
559
-
560
- ```ruby
561
- expect(:get => "/widgets").to route_to(:controller => "widgets", :action => "index")
562
- ```
563
-
564
- ### `be_routable`
565
-
566
- Passes if the path is recognized by Rails' routing. This is primarily intended
567
- to be used with `not_to` to specify standard CRUD routes which should not be
568
- routable.
569
-
570
- ```ruby
571
- expect(:get => "/widgets/1/edit").not_to be_routable
572
- ```
573
-
574
- ### `have_http_status`
575
-
576
- - Passes if `response` has a matching HTTP status code
577
- - The following symbolic status codes are allowed:
578
- - `Rack::Utils::SYMBOL_TO_STATUS_CODE`
579
- - One of the defined `ActionDispatch::TestResponse` aliases:
580
- - `:error`
581
- - `:missing`
582
- - `:redirect`
583
- - `:success`
584
- - Available in controller, feature, and request specs.
585
-
586
- In controller and request specs, apply to the `response` object:
587
-
588
- ```ruby
589
- expect(response).to have_http_status(201)
590
- expect(response).not_to have_http_status(:created)
591
- ```
592
-
593
- In feature specs, apply to the `page` object:
594
-
595
- ```ruby
596
- expect(page).to have_http_status(:success)
597
- ```
598
-
599
- ## `rake` tasks
600
-
601
- Several rake tasks are provided as a convenience for working with RSpec. To run
602
- the entire spec suite use `rake spec`. To run a subset of specs use the
603
- associated type task, for example `rake spec:models`.
604
-
605
- A full list of the available rake tasks can be seen by running `rake -T | grep
606
- spec`.
356
+ ## Contributing
607
357
 
608
- ### Customizing `rake` tasks
358
+ - [Build details](BUILD_DETAIL.md)
359
+ - [Code of Conduct](CODE_OF_CONDUCT.md)
360
+ - [Detailed contributing guide](CONTRIBUTING.md)
609
361
 
610
- If you want to customize the behavior of `rake spec`, you may [define your own
611
- task in the `Rakefile` for your
612
- project](https://www.relishapp.com/rspec/rspec-core/docs/command-line/rake-task).
613
- However, you must first clear the task that rspec-rails defined:
362
+ Once you’ve cloned the repo and [set up the environment](DEVELOPMENT.md),
363
+ you can run the specs and Cucumber features, or submit a pull request.
614
364
 
615
- ```ruby
616
- task("spec").clear
617
- ```
365
+ ## See Also
618
366
 
367
+ ### RSpec base libraries
619
368
 
620
- ## Also see
369
+ * <https://github.com/rspec/rspec>
370
+ * <https://github.com/rspec/rspec-core>
371
+ * <https://github.com/rspec/rspec-expectations>
372
+ * <https://github.com/rspec/rspec-mocks>
621
373
 
622
- * [https://github.com/rspec/rspec](https://github.com/rspec/rspec)
623
- * [https://github.com/rspec/rspec-core](https://github.com/rspec/rspec-core)
624
- * [https://github.com/rspec/rspec-expectations](https://github.com/rspec/rspec-expectations)
625
- * [https://github.com/rspec/rspec-mocks](https://github.com/rspec/rspec-mocks)
374
+ ### Recommended third-party extensions
626
375
 
627
- ## Feature Requests & Bugs
376
+ * [FactoryBot](https://github.com/thoughtbot/factory_bot)
377
+ * [Capybara](https://github.com/teamcapybara/capybara)
378
+ (Included by default in Rails 5.1+.
379
+ Note that [additional configuration is required][] to use the Capybara DSL
380
+ anywhere other than system specs and feature specs.)
628
381
 
629
- See <http://github.com/rspec/rspec-rails/issues>
382
+ [additional configuration is required]: https://rubydoc.info/gems/rspec-rails/file/Capybara.md