rspec-rails 4.0.0.beta2 → 6.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/Capybara.md +5 -54
  4. data/Changelog.md +352 -86
  5. data/README.md +76 -65
  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 +3 -3
  12. data/lib/generators/rspec/{generators → generator}/generator_generator.rb +3 -3
  13. data/lib/generators/rspec/helper/helper_generator.rb +2 -2
  14. data/lib/generators/rspec/install/install_generator.rb +23 -6
  15. data/lib/generators/rspec/install/templates/spec/rails_helper.rb +27 -21
  16. data/lib/generators/rspec/integration/integration_generator.rb +13 -6
  17. data/lib/generators/rspec/job/job_generator.rb +2 -1
  18. data/lib/generators/rspec/job/templates/job_spec.rb.erb +1 -1
  19. data/lib/generators/rspec/mailbox/mailbox_generator.rb +14 -0
  20. data/lib/generators/rspec/mailbox/templates/mailbox_spec.rb.erb +7 -0
  21. data/lib/generators/rspec/mailer/mailer_generator.rb +4 -4
  22. data/lib/generators/rspec/mailer/templates/mailer_spec.rb +2 -2
  23. data/lib/generators/rspec/mailer/templates/preview.rb +1 -1
  24. data/lib/generators/rspec/model/model_generator.rb +7 -7
  25. data/lib/generators/rspec/model/templates/fixtures.yml +1 -1
  26. data/lib/generators/rspec/request/request_generator.rb +10 -3
  27. data/lib/generators/rspec/scaffold/scaffold_generator.rb +33 -21
  28. data/lib/generators/rspec/scaffold/templates/api_controller_spec.rb +13 -49
  29. data/lib/generators/rspec/scaffold/templates/api_request_spec.rb +131 -0
  30. data/lib/generators/rspec/scaffold/templates/controller_spec.rb +25 -58
  31. data/lib/generators/rspec/scaffold/templates/edit_spec.rb +9 -9
  32. data/lib/generators/rspec/scaffold/templates/index_spec.rb +3 -2
  33. data/lib/generators/rspec/scaffold/templates/new_spec.rb +2 -6
  34. data/lib/generators/rspec/scaffold/templates/request_spec.rb +153 -0
  35. data/lib/generators/rspec/scaffold/templates/routing_spec.rb +8 -10
  36. data/lib/generators/rspec/scaffold/templates/show_spec.rb +2 -2
  37. data/lib/generators/rspec/system/system_generator.rb +14 -16
  38. data/lib/generators/rspec/view/view_generator.rb +4 -4
  39. data/lib/generators/rspec.rb +16 -5
  40. data/lib/rspec/rails/adapters.rb +21 -76
  41. data/lib/rspec/rails/configuration.rb +112 -38
  42. data/lib/rspec/rails/example/channel_example_group.rb +93 -0
  43. data/lib/rspec/rails/example/controller_example_group.rb +5 -4
  44. data/lib/rspec/rails/example/feature_example_group.rb +6 -26
  45. data/lib/rspec/rails/example/helper_example_group.rb +2 -10
  46. data/lib/rspec/rails/example/mailbox_example_group.rb +80 -0
  47. data/lib/rspec/rails/example/mailer_example_group.rb +2 -2
  48. data/lib/rspec/rails/example/rails_example_group.rb +9 -1
  49. data/lib/rspec/rails/example/request_example_group.rb +1 -4
  50. data/lib/rspec/rails/example/system_example_group.rb +80 -14
  51. data/lib/rspec/rails/example/view_example_group.rb +40 -28
  52. data/lib/rspec/rails/example.rb +2 -0
  53. data/lib/rspec/rails/extensions/active_record/proxy.rb +5 -10
  54. data/lib/rspec/rails/feature_check.rb +15 -22
  55. data/lib/rspec/rails/file_fixture_support.rb +11 -10
  56. data/lib/rspec/rails/fixture_file_upload_support.rb +20 -15
  57. data/lib/rspec/rails/fixture_support.rb +65 -34
  58. data/lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb +173 -0
  59. data/lib/rspec/rails/matchers/action_cable/have_streams.rb +58 -0
  60. data/lib/rspec/rails/matchers/action_cable.rb +65 -0
  61. data/lib/rspec/rails/matchers/action_mailbox.rb +73 -0
  62. data/lib/rspec/rails/matchers/active_job.rb +171 -24
  63. data/lib/rspec/rails/matchers/base_matcher.rb +4 -10
  64. data/lib/rspec/rails/matchers/have_enqueued_mail.rb +81 -27
  65. data/lib/rspec/rails/matchers/have_http_status.rb +12 -12
  66. data/lib/rspec/rails/matchers/relation_match_array.rb +1 -1
  67. data/lib/rspec/rails/matchers/routing_matchers.rb +11 -11
  68. data/lib/rspec/rails/matchers/send_email.rb +122 -0
  69. data/lib/rspec/rails/matchers.rb +11 -0
  70. data/lib/rspec/rails/tasks/rspec.rake +7 -17
  71. data/lib/rspec/rails/vendor/capybara.rb +10 -17
  72. data/lib/rspec/rails/version.rb +1 -1
  73. data/lib/rspec/rails/view_assigns.rb +0 -18
  74. data/lib/rspec/rails/view_path_builder.rb +1 -1
  75. data/lib/rspec/rails/view_rendering.rb +16 -14
  76. data/lib/rspec-rails.rb +19 -20
  77. data.tar.gz.sig +0 -0
  78. metadata +53 -40
  79. metadata.gz.sig +0 -0
  80. data/lib/generators/rspec/observer/observer_generator.rb +0 -13
  81. /data/lib/generators/rspec/{generators → generator}/templates/generator_spec.rb +0 -0
  82. /data/lib/generators/rspec/{integration → request}/templates/request_spec.rb +0 -0
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # rspec-rails [![Build Status][]][travis-ci] [![Code Climate][]][code-climate] [![Gem Version][]](gem-version)
1
+ # rspec-rails [![Code Climate][]][code-climate] [![Gem Version][]][gem-version]
2
2
 
3
3
  `rspec-rails` brings the [RSpec][] testing framework to [Ruby on Rails][]
4
4
  as a drop-in alternative to its default testing framework, Minitest.
@@ -8,34 +8,46 @@ They’re also specifications (or _specs,_ for short):
8
8
  detailed explanations of how the application is supposed to behave,
9
9
  expressed in plain English.
10
10
 
11
- Use **[`rspec-rails` 1.x][]** for Rails 2.x.
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.
12
17
 
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://img.shields.io/codeclimate/github/rspec/rspec-rails.svg
18
+ [Code Climate]: https://codeclimate.com/github/rspec/rspec-rails.svg
16
19
  [code-climate]: https://codeclimate.com/github/rspec/rspec-rails
17
20
  [Gem Version]: https://badge.fury.io/rb/rspec-rails.svg
18
21
  [gem-version]: https://badge.fury.io/rb/rspec-rails
19
22
  [RSpec]: https://rspec.info/
20
23
  [Ruby on Rails]: https://rubyonrails.org/
21
24
  [`rspec-rails` 1.x]: https://github.com/dchelimsky/rspec-rails
25
+ [`rspec-rails` 3.x]: https://github.com/rspec/rspec-rails/tree/3-9-maintenance
26
+ [`rspec-rails` 4.x]: https://github.com/rspec/rspec-rails/tree/4-1-maintenance
27
+ [`rspec-rails` 5.x]: https://github.com/rspec/rspec-rails/tree/5-1-maintenance
28
+ [`rspec-rails` 6.x]: https://github.com/rspec/rspec-rails/tree/6-1-maintenance
29
+ [RSpec Rails new versioning strategy]: https://github.com/rspec/rspec-rails/blob/main/rfcs/versioning-strategy.md
22
30
 
23
31
  ## Installation
24
32
 
33
+ **IMPORTANT** This README / branch refers to the 6.1.x stable release series, only bugfixes from this series will
34
+ be added here. See the [`main` branch on Github](https://github.com/rspec/rspec-rails/tree/main) if you want or
35
+ require the latest unstable features.
36
+
25
37
  1. Add `rspec-rails` to **both** the `:development` and `:test` groups
26
38
  of your app’s `Gemfile`:
27
39
 
28
40
  ```ruby
29
- # Run against the latest stable release
41
+ # Run against this stable release
30
42
  group :development, :test do
31
- gem 'rspec-rails', '~> 3.8'
43
+ gem 'rspec-rails', '~> 6.1.0'
32
44
  end
33
45
 
34
- # Or, run against the master branch
35
- # (requires master-branch versions of all related RSpec libraries)
46
+ # Or, run against the main branch
47
+ # (requires main-branch versions of all related RSpec libraries)
36
48
  group :development, :test do
37
49
  %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'
50
+ gem lib, git: "https://github.com/rspec/#{lib}.git", branch: 'main'
39
51
  end
40
52
  end
41
53
  ```
@@ -75,8 +87,8 @@ read the [`rspec-rails` upgrade notes][] to find out what to watch out for.
75
87
 
76
88
  Be sure to check the general [RSpec upgrade notes][] as well.
77
89
 
78
- [`rspec-rails` upgrade notes]: https://www.relishapp.com/rspec/rspec-rails/docs/upgrade
79
- [RSpec upgrade notes]: https://relishapp.com/rspec/docs/upgrade
90
+ [`rspec-rails` upgrade notes]: https://rspec.info/features/6-0/rspec-rails/upgrade
91
+ [RSpec upgrade notes]: https://rspec.info/upgrading-from-rspec-2/
80
92
 
81
93
  ## Usage
82
94
 
@@ -119,8 +131,7 @@ $ bundle exec rspec --help
119
131
  ```
120
132
 
121
133
  **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):
134
+ you can generate a binstub at `bin/rspec` and use that instead:
124
135
 
125
136
  ```sh
126
137
  $ bundle binstubs rspec-core
@@ -171,7 +182,7 @@ rspec ./spec/models/post_spec.rb:3 # Post before publication cannot have comment
171
182
  For an in-depth look at the RSpec DSL, including lots of examples,
172
183
  read the official Cucumber documentation for [RSpec Core][].
173
184
 
174
- [RSpec Core]: https://relishapp.com/rspec/rspec-core/docs
185
+ [RSpec Core]: https://rspec.info/features/3-12/rspec-core
175
186
 
176
187
  ### Helpful Rails Matchers
177
188
 
@@ -183,37 +194,37 @@ In addition to [the matchers that come standard in RSpec][],
183
194
  here are some extras that make it easier
184
195
  to test the various parts of a Rails system:
185
196
 
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` |
197
+ | RSpec matcher | Delegates to | Available in | Notes |
198
+ | ------------------------ | ------------------- | ------------------------------- | -------------------------------------------------------- |
199
+ | [`be_a_new`][] | | all | primarily intended for controller specs |
200
+ | [`render_template`][] | `assert_template` | request / controller / view | use with `expect(response).to` |
201
+ | [`redirect_to`][] | `assert_redirect` | request / controller | use with `expect(response).to` |
202
+ | [`route_to`] | `assert_recognizes` | routing / controller | use with `expect(...).to route_to` |
203
+ | [`be_routable`] | | routing / controller | use with `expect(...).not_to be_routable` |
204
+ | [`have_http_status`][] | | request / controller / feature | |
205
+ | [`match_array`][] | | all | for comparing arrays of ActiveRecord objects |
206
+ | [`have_been_enqueued`][] | | all | requires config: `ActiveJob::Base.queue_adapter = :test` |
207
+ | [`have_enqueued_job`][] | | all | requires config: `ActiveJob::Base.queue_adapter = :test` |
197
208
 
198
209
  Follow the links above for examples of how each matcher is used.
199
210
 
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
211
+ [the matchers that come standard in RSpec]: https://rspec.info/features/3-12/rspec-expectations/built-in-matchers
212
+ [`be_a_new`]: https://rspec.info/features/6-0/rspec-rails/matchers/new-record-matcher
213
+ [`render_template`]: https://rspec.info/features/6-0/rspec-rails/matchers/render-template-matcher
214
+ [`redirect_to`]: https://rspec.info/features/6-0/rspec-rails/matchers/redirect-to-matcher
215
+ [`route_to`]: https://rspec.info/features/6-0/rspec-rails/routing-specs/route-to-matcher
216
+ [`be_routable`]: https://rspec.info/features/6-0/rspec-rails/routing-specs/be-routable-matcher
217
+ [`have_http_status`]: https://rspec.info/features/6-0/rspec-rails/matchers/have-http-status-matcher
218
+ [`match_array`]: https://rspec.info/features/6-0/rspec-rails/matchers/relation-match-array
219
+ [`have_been_enqueued`]: https://rspec.info/features/6-0/rspec-rails/matchers/have-been-enqueued-matcher
220
+ [`have_enqueued_job`]: https://rspec.info/features/6-0/rspec-rails/matchers/have-enqueued-job-matcher
210
221
 
211
222
  ### What else does RSpec Rails add?
212
223
 
213
224
  For a comprehensive look at RSpec Rails’ features,
214
225
  read the [official Cucumber documentation][].
215
226
 
216
- [official Cucumber documentation]: https://relishapp.com/rspec/rspec-rails/docs
227
+ [official Cucumber documentation]: https://rspec.info/features/6-0/rspec-rails
217
228
 
218
229
  ## What tests should I write?
219
230
 
@@ -223,18 +234,18 @@ Each one inherits from one of Rails’ built-in `TestCase` classes,
223
234
  meaning the helper methods provided by default in Rails tests
224
235
  are available in RSpec, as well.
225
236
 
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`][] |
237
+ | Spec type | Corresponding Rails test class |
238
+ | -------------- | -------------------------------- |
239
+ | [model][] | |
240
+ | [controller][] | [`ActionController::TestCase`][] |
241
+ | [mailer][] | `ActionMailer::TestCase` |
242
+ | [job][] | |
243
+ | [view][] | `ActionView::TestCase` |
244
+ | [routing][] | |
245
+ | [helper][] | `ActionView::TestCase` |
246
+ | [request][] | [`ActionDispatch::IntegrationTest`][] |
247
+ | [feature][] | |
248
+ | [system][] | [`ActionDispatch::SystemTestCase`][] |
238
249
 
239
250
  Follow the links above to see examples of each spec type,
240
251
  or for official Rails API documentation on the given `TestCase` class.
@@ -258,20 +269,20 @@ RSpec.describe User, type: :model do
258
269
  ...
259
270
  ```
260
271
 
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
272
+ [request]: https://rspec.info/features/6-0/rspec-rails/request-specs/request-spec
273
+ [feature]: https://rspec.info/features/6-0/rspec-rails/feature-specs/feature-spec
274
+ [system]: https://rspec.info/features/6-0/rspec-rails/system-specs/system-specs
275
+ [model]: https://rspec.info/features/6-0/rspec-rails/model-specs
276
+ [controller]: https://rspec.info/features/6-0/rspec-rails/controller-specs
277
+ [mailer]: https://rspec.info/features/6-0/rspec-rails/mailer-specs
278
+ [job]: https://rspec.info/features/6-0/rspec-rails/job-specs/job-spec
279
+ [view]: https://rspec.info/features/6-0/rspec-rails/view-specs/view-spec
280
+ [routing]: https://rspec.info/features/6-0/rspec-rails/routing-specs
281
+ [helper]: https://rspec.info/features/6-0/rspec-rails/helper-specs/helper-spec
271
282
  [`ActionDispatch::IntegrationTest`]: https://api.rubyonrails.org/classes/ActionDispatch/IntegrationTest.html
272
283
  [`ActionDispatch::SystemTestCase`]: https://api.rubyonrails.org/classes/ActionDispatch/SystemTestCase.html
273
284
  [`ActionController::TestCase`]: https://api.rubyonrails.org/classes/ActionController/TestCase.html
274
- [in the appropriate folder]: https://relishapp.com/rspec/rspec-rails/docs/directory-structure
285
+ [in the appropriate folder]: https://rspec.info/features/6-0/rspec-rails/directory-structure
275
286
 
276
287
  ### System specs, feature specs, request specs–what’s the difference?
277
288
 
@@ -354,15 +365,15 @@ you can run the specs and Cucumber features, or submit a pull request.
354
365
 
355
366
  ### RSpec base libraries
356
367
 
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>
368
+ * https://github.com/rspec/rspec
369
+ * https://github.com/rspec/rspec-core
370
+ * https://github.com/rspec/rspec-expectations
371
+ * https://github.com/rspec/rspec-mocks
361
372
 
362
373
  ### Recommended third-party extensions
363
374
 
364
375
  * [FactoryBot](https://github.com/thoughtbot/factory_bot)
365
- * [Capybara](https://github.com/jnicklas/capybara)
376
+ * [Capybara](https://github.com/teamcapybara/capybara)
366
377
  (Included by default in Rails 5.1+.
367
378
  Note that [additional configuration is required][] to use the Capybara DSL
368
379
  anywhere other than system specs and feature specs.)
@@ -0,0 +1,12 @@
1
+ require 'generators/rspec'
2
+
3
+ module Rspec
4
+ module Generators
5
+ # @private
6
+ class ChannelGenerator < Base
7
+ def create_channel_spec
8
+ template 'channel_spec.rb.erb', target_path('channels', class_path, "#{file_name}_channel_spec.rb")
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,7 +1,7 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  <% module_namespacing do -%>
4
- RSpec.describe <%= class_name %>Observer, <%= type_metatag(:observer) %> do
4
+ RSpec.describe <%= class_name %>Channel, <%= type_metatag(:channel) %> do
5
5
  pending "add some examples to (or delete) #{__FILE__}"
6
6
  end
7
7
  <% end -%>
@@ -4,21 +4,30 @@ module Rspec
4
4
  module Generators
5
5
  # @private
6
6
  class ControllerGenerator < Base
7
- argument :actions, :type => :array, :default => [], :banner => "action action"
7
+ argument :actions, type: :array, default: [], banner: "action action"
8
8
 
9
- class_option :template_engine, :desc => "Template engine to generate view files"
10
- class_option :controller_specs, :type => :boolean, :default => true
11
- class_option :view_specs, :type => :boolean, :default => true
9
+ class_option :template_engine, desc: "Template engine to generate view files"
10
+ class_option :request_specs, type: :boolean, default: true, desc: "Generate request specs"
11
+ class_option :controller_specs, type: :boolean, default: false, desc: "Generate controller specs"
12
+ class_option :view_specs, type: :boolean, default: true, desc: "Generate view specs"
13
+ class_option :routing_specs, type: :boolean, default: false, desc: "Generate routing specs"
14
+
15
+ def generate_request_spec
16
+ return unless options[:request_specs]
17
+
18
+ template 'request_spec.rb',
19
+ target_path('requests', class_path, "#{file_name}_spec.rb")
20
+ end
12
21
 
13
22
  def generate_controller_spec
14
23
  return unless options[:controller_specs]
15
24
 
16
25
  template 'controller_spec.rb',
17
- File.join('spec/controllers', class_path, "#{file_name}_controller_spec.rb")
26
+ target_path('controllers', class_path, "#{file_name}_controller_spec.rb")
18
27
  end
19
28
 
20
29
  def generate_view_specs
21
- return if actions.empty?
30
+ return if actions.empty? && behavior == :invoke
22
31
  return unless options[:view_specs] && options[:template_engine]
23
32
 
24
33
  empty_directory File.join("spec", "views", file_path)
@@ -26,9 +35,17 @@ module Rspec
26
35
  actions.each do |action|
27
36
  @action = action
28
37
  template 'view_spec.rb',
29
- File.join("spec", "views", file_path, "#{@action}.html.#{options[:template_engine]}_spec.rb")
38
+ target_path('views', file_path, "#{@action}.html.#{options[:template_engine]}_spec.rb")
30
39
  end
31
40
  end
41
+
42
+ def generate_routing_spec
43
+ return if actions.empty?
44
+ return unless options[:routing_specs]
45
+
46
+ template 'routing_spec.rb',
47
+ target_path('routing', class_path, "#{file_name}_routing_spec.rb")
48
+ end
32
49
  end
33
50
  end
34
51
  end
@@ -0,0 +1,19 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe "<%= class_name.pluralize %>", <%= type_metatag(:request) %> do
4
+ <% namespaced_path = regular_class_path.join('/') -%>
5
+ <% if actions.empty? -%>
6
+ describe "GET /index" do
7
+ pending "add some examples (or delete) #{__FILE__}"
8
+ end
9
+ <% end -%>
10
+ <% for action in actions -%>
11
+ describe "GET /<%= action %>" do
12
+ it "returns http success" do
13
+ get "<%= "/#{namespaced_path}" if namespaced_path != '' %>/<%= file_name %>/<%= action %>"
14
+ expect(response).to have_http_status(:success)
15
+ end
16
+ end
17
+
18
+ <% end -%>
19
+ end
@@ -0,0 +1,13 @@
1
+ require 'rails_helper'
2
+
3
+ <% module_namespacing do -%>
4
+ RSpec.describe '<%= class_name %>Controller', <%= type_metatag(:routing) %> do
5
+ describe 'routing' do
6
+ <% for action in actions -%>
7
+ it 'routes to #<%= action %>' do
8
+ expect(get: "/<%= class_name.underscore %>/<%= action %>").to route_to("<%= class_name.underscore %>#<%= action %>")
9
+ end
10
+ <% end -%>
11
+ end
12
+ end
13
+ <% end -%>
@@ -4,13 +4,13 @@ module Rspec
4
4
  module Generators
5
5
  # @private
6
6
  class FeatureGenerator < Base
7
- class_option :feature_specs, :type => :boolean, :default => true, :desc => "Generate feature specs"
8
- class_option :singularize, :type => :boolean, :default => false, :desc => "Singularize the generated feature"
7
+ class_option :feature_specs, type: :boolean, default: true, desc: "Generate feature specs"
8
+ class_option :singularize, type: :boolean, default: false, desc: "Singularize the generated feature"
9
9
 
10
10
  def generate_feature_spec
11
11
  return unless options[:feature_specs]
12
12
 
13
- template template_name, File.join('spec/features', class_path, filename)
13
+ template template_name, target_path('features', class_path, filename)
14
14
  end
15
15
 
16
16
  def template_name
@@ -3,13 +3,13 @@ require 'generators/rspec'
3
3
  module Rspec
4
4
  module Generators
5
5
  # @private
6
- class GeneratorsGenerator < Base
7
- class_option :generator_specs, :type => :boolean, :default => false, :desc => "Generate generator specs"
6
+ class GeneratorGenerator < Base
7
+ class_option :generator_specs, type: :boolean, default: true, desc: "Generate generator specs"
8
8
 
9
9
  def generate_generator_spec
10
10
  return unless options[:generator_specs]
11
11
 
12
- template template_name, File.join('spec/generator', class_path, filename)
12
+ template template_name, target_path('generator', class_path, filename)
13
13
  end
14
14
 
15
15
  def template_name
@@ -4,12 +4,12 @@ module Rspec
4
4
  module Generators
5
5
  # @private
6
6
  class HelperGenerator < Base
7
- class_option :helper_specs, :type => :boolean, :default => true
7
+ class_option :helper_specs, type: :boolean, default: true
8
8
 
9
9
  def generate_helper_spec
10
10
  return unless options[:helper_specs]
11
11
 
12
- template 'helper_spec.rb', File.join('spec/helpers', class_path, "#{file_name}_helper_spec.rb")
12
+ template 'helper_spec.rb', target_path('helpers', class_path, "#{file_name}_helper_spec.rb")
13
13
  end
14
14
  end
15
15
  end
@@ -12,6 +12,8 @@ Description:
12
12
  Copy rspec files to your application.
13
13
  DESC
14
14
 
15
+ class_option :default_path, type: :string, default: 'spec'
16
+
15
17
  def self.source_root
16
18
  @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
17
19
  end
@@ -20,20 +22,20 @@ DESC
20
22
  Dir.mktmpdir do |dir|
21
23
  generate_rspec_init dir
22
24
  template File.join(dir, '.rspec'), '.rspec'
23
- directory File.join(dir, 'spec'), 'spec'
25
+ directory File.join(dir, 'spec'), default_path
24
26
  end
25
27
  end
26
28
 
27
29
  def copy_rails_files
28
- template 'spec/rails_helper.rb'
30
+ template 'spec/rails_helper.rb', "#{default_path}/rails_helper.rb"
29
31
  end
30
32
 
31
33
  private
32
34
 
33
35
  def generate_rspec_init(tmpdir)
34
36
  initializer = ::RSpec::Core::ProjectInitializer.new(
35
- :destination => tmpdir,
36
- :report_stream => StringIO.new
37
+ destination: tmpdir,
38
+ report_stream: StringIO.new
37
39
  )
38
40
  initializer.run
39
41
 
@@ -41,13 +43,19 @@ DESC
41
43
 
42
44
  replace_generator_command(spec_helper_path)
43
45
  remove_warnings_configuration(spec_helper_path)
46
+
47
+ unless default_path == "spec"
48
+ dot_rspec_path = File.join(tmpdir, '.rspec')
49
+
50
+ append_default_path(dot_rspec_path)
51
+ end
44
52
  end
45
53
 
46
54
  def replace_generator_command(spec_helper_path)
47
55
  gsub_file spec_helper_path,
48
56
  'rspec --init',
49
57
  'rails generate rspec:install',
50
- :verbose => false
58
+ verbose: false
51
59
  end
52
60
 
53
61
  def remove_warnings_configuration(spec_helper_path)
@@ -56,7 +64,16 @@ DESC
56
64
  gsub_file spec_helper_path,
57
65
  /#{empty_line}(#{comment_line})+\s+config\.warnings = true\n/,
58
66
  '',
59
- :verbose => false
67
+ verbose: false
68
+ end
69
+
70
+ def append_default_path(dot_rspec_path)
71
+ append_to_file dot_rspec_path,
72
+ "--default-path #{default_path}"
73
+ end
74
+
75
+ def default_path
76
+ options[:default_path]
60
77
  end
61
78
  end
62
79
  end
@@ -1,11 +1,7 @@
1
1
  # This file is copied to spec/ when you run 'rails generate rspec:install'
2
2
  require 'spec_helper'
3
3
  ENV['RAILS_ENV'] ||= 'test'
4
- <% if RUBY_VERSION >= '2.0.0' %>
5
- require File.expand_path('../config/environment', __dir__)
6
- <% else %>
7
- require File.expand_path('../../config/environment', __FILE__)
8
- <% end %>
4
+ require_relative '../config/environment'
9
5
  # Prevent database truncation if the environment is production
10
6
  abort("The Rails environment is running in production mode!") if Rails.env.production?
11
7
  require 'rspec/rails'
@@ -24,37 +20,47 @@ require 'rspec/rails'
24
20
  # directory. Alternatively, in the individual `*_spec.rb` files, manually
25
21
  # require only the support files necessary.
26
22
  #
27
- # Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
23
+ # Rails.root.glob('spec/support/**/*.rb').sort.each { |f| require f }
28
24
 
29
- <% if RSpec::Rails::FeatureCheck.can_maintain_test_schema? -%>
25
+ <% if RSpec::Rails::FeatureCheck.has_active_record_migration? -%>
30
26
  # Checks for pending migrations and applies them before tests are run.
31
27
  # If you are not using ActiveRecord, you can remove these lines.
32
28
  begin
33
29
  ActiveRecord::Migration.maintain_test_schema!
34
30
  rescue ActiveRecord::PendingMigrationError => e
35
- puts e.to_s.strip
36
- exit 1
37
- end
38
- <% elsif RSpec::Rails::FeatureCheck.can_check_pending_migrations? -%>
39
- # Checks for pending migrations before tests are run.
40
- # If you are not using ActiveRecord, you can remove these lines.
41
- begin
42
- ActiveRecord::Migration.check_pending!
43
- rescue ActiveRecord::PendingMigrationError => e
44
- puts e.to_s.strip
45
- exit 1
31
+ abort e.to_s.strip
46
32
  end
47
33
  <% end -%>
48
34
  RSpec.configure do |config|
49
35
  <% if RSpec::Rails::FeatureCheck.has_active_record? -%>
50
36
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
51
- config.fixture_path = "#{::Rails.root}/spec/fixtures"
37
+ <% if ::Rails::VERSION::STRING < "7.1.0" -%>
38
+ config.fixture_path = Rails.root.join('spec/fixtures')
39
+ <% else -%>
40
+ config.fixture_paths = [
41
+ Rails.root.join('spec/fixtures')
42
+ ]
43
+ <% end -%>
52
44
 
53
45
  # If you're not using ActiveRecord, or you'd prefer not to run each of your
54
46
  # examples within a transaction, remove the following line or assign false
55
47
  # instead of true.
56
48
  config.use_transactional_fixtures = true
57
49
 
50
+ # You can uncomment this line to turn off ActiveRecord support entirely.
51
+ # config.use_active_record = false
52
+
53
+ <% else -%>
54
+ # Remove this line to enable support for ActiveRecord
55
+ config.use_active_record = false
56
+
57
+ # If you enable ActiveRecord support you should uncomment these lines,
58
+ # note if you'd prefer not to run each example within a transaction, you
59
+ # should set use_transactional_fixtures to false.
60
+ #
61
+ # config.fixture_path = Rails.root.join('spec/fixtures')
62
+ # config.use_transactional_fixtures = true
63
+
58
64
  <% end -%>
59
65
  # RSpec Rails can automatically mix in different behaviours to your tests
60
66
  # based on their file location, for example enabling you to call `get` and
@@ -63,12 +69,12 @@ RSpec.configure do |config|
63
69
  # You can disable this behaviour by removing the line below, and instead
64
70
  # explicitly tag your specs with their type, e.g.:
65
71
  #
66
- # RSpec.describe UsersController, :type => :controller do
72
+ # RSpec.describe UsersController, type: :controller do
67
73
  # # ...
68
74
  # end
69
75
  #
70
76
  # The different available types are documented in the features, such as in
71
- # https://relishapp.com/rspec/rspec-rails/docs
77
+ # https://rspec.info/features/6-0/rspec-rails
72
78
  config.infer_spec_type_from_file_location!
73
79
 
74
80
  # Filter lines from Rails gems in backtraces.
@@ -1,21 +1,28 @@
1
1
  require 'generators/rspec'
2
+ require 'rspec/core/warnings'
2
3
 
3
4
  module Rspec
4
5
  module Generators
5
6
  # @private
6
7
  class IntegrationGenerator < Base
7
- # Add a deprecation for this class, before rspec-rails 4, to use the
8
- # `RequestGenerator` instead
9
8
  class_option :request_specs,
10
- :type => :boolean,
11
- :default => true,
12
- :desc => "Generate request specs"
9
+ type: :boolean,
10
+ default: true,
11
+ desc: "Generate request specs"
12
+
13
+ source_paths << File.expand_path('../request/templates', __dir__)
13
14
 
14
15
  def generate_request_spec
15
16
  return unless options[:request_specs]
16
17
 
18
+ RSpec.warn_deprecation <<-WARNING.gsub(/\s*\|/, ' ')
19
+ |The integration generator is deprecated
20
+ |and will be deleted in RSpec-Rails 7.
21
+ |Please use the request generator instead.
22
+ WARNING
23
+
17
24
  template 'request_spec.rb',
18
- File.join('spec/requests', "#{name.underscore.pluralize}_spec.rb")
25
+ target_path('requests', "#{name.underscore.pluralize}_spec.rb")
19
26
  end
20
27
  end
21
28
  end
@@ -5,7 +5,8 @@ module Rspec
5
5
  # @private
6
6
  class JobGenerator < Base
7
7
  def create_job_spec
8
- template 'job_spec.rb.erb', File.join('spec/jobs', class_path, "#{file_name}_job_spec.rb")
8
+ file_suffix = file_name.end_with?('job') ? 'spec.rb' : 'job_spec.rb'
9
+ template 'job_spec.rb.erb', target_path('jobs', class_path, [file_name, file_suffix].join('_'))
9
10
  end
10
11
  end
11
12
  end
@@ -1,7 +1,7 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  <% module_namespacing do -%>
4
- RSpec.describe <%= class_name %>Job, <%= type_metatag(:job) %> do
4
+ RSpec.describe <%= class_name %><%= "Job" unless class_name.end_with?("Job")%>, <%= type_metatag(:job) %> do
5
5
  pending "add some examples to (or delete) #{__FILE__}"
6
6
  end
7
7
  <% end -%>
@@ -0,0 +1,14 @@
1
+ require 'generators/rspec'
2
+
3
+ module Rspec
4
+ module Generators
5
+ # @private
6
+ class MailboxGenerator < Base
7
+ def create_mailbox_spec
8
+ template('mailbox_spec.rb.erb',
9
+ target_path('mailboxes', class_path, "#{file_name}_mailbox_spec.rb")
10
+ )
11
+ end
12
+ end
13
+ end
14
+ end