rspec-rails 3.9.0 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/Capybara.md +5 -54
  4. data/Changelog.md +156 -78
  5. data/README.md +8 -7
  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 +21 -4
  9. data/lib/generators/rspec/controller/templates/request_spec.rb +14 -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/{generators → generator}/generator_generator.rb +2 -2
  13. data/lib/generators/rspec/{generators → generator}/templates/generator_spec.rb +0 -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 +17 -16
  17. data/lib/generators/rspec/integration/integration_generator.rb +3 -3
  18. data/lib/generators/rspec/mailbox/mailbox_generator.rb +14 -0
  19. data/lib/generators/rspec/mailbox/templates/mailbox_spec.rb.erb +7 -0
  20. data/lib/generators/rspec/mailer/mailer_generator.rb +2 -1
  21. data/lib/generators/rspec/model/model_generator.rb +5 -4
  22. data/lib/generators/rspec/model/templates/fixtures.yml +1 -1
  23. data/lib/generators/rspec/request/request_generator.rb +1 -1
  24. data/lib/generators/rspec/scaffold/scaffold_generator.rb +29 -19
  25. data/lib/generators/rspec/scaffold/templates/api_controller_spec.rb +0 -36
  26. data/lib/generators/rspec/scaffold/templates/api_request_spec.rb +131 -0
  27. data/lib/generators/rspec/scaffold/templates/controller_spec.rb +10 -10
  28. data/lib/generators/rspec/scaffold/templates/edit_spec.rb +1 -1
  29. data/lib/generators/rspec/scaffold/templates/index_spec.rb +2 -2
  30. data/lib/generators/rspec/scaffold/templates/new_spec.rb +1 -1
  31. data/lib/generators/rspec/scaffold/templates/request_spec.rb +133 -0
  32. data/lib/generators/rspec/scaffold/templates/routing_spec.rb +8 -10
  33. data/lib/generators/rspec/scaffold/templates/show_spec.rb +1 -1
  34. data/lib/generators/rspec/system/system_generator.rb +1 -1
  35. data/lib/generators/rspec/view/view_generator.rb +2 -2
  36. data/lib/generators/rspec.rb +0 -6
  37. data/lib/rspec/rails/adapters.rb +11 -76
  38. data/lib/rspec/rails/configuration.rb +43 -33
  39. data/lib/rspec/rails/example/channel_example_group.rb +93 -0
  40. data/lib/rspec/rails/example/controller_example_group.rb +4 -4
  41. data/lib/rspec/rails/example/feature_example_group.rb +6 -26
  42. data/lib/rspec/rails/example/helper_example_group.rb +2 -9
  43. data/lib/rspec/rails/example/mailbox_example_group.rb +80 -0
  44. data/lib/rspec/rails/example/mailer_example_group.rb +2 -2
  45. data/lib/rspec/rails/example/rails_example_group.rb +1 -1
  46. data/lib/rspec/rails/example/system_example_group.rb +26 -10
  47. data/lib/rspec/rails/example/view_example_group.rb +38 -27
  48. data/lib/rspec/rails/example.rb +2 -0
  49. data/lib/rspec/rails/extensions/active_record/proxy.rb +1 -9
  50. data/lib/rspec/rails/feature_check.rb +12 -29
  51. data/lib/rspec/rails/fixture_file_upload_support.rb +1 -1
  52. data/lib/rspec/rails/fixture_support.rb +37 -31
  53. data/lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb +170 -0
  54. data/lib/rspec/rails/matchers/action_cable/have_streams.rb +58 -0
  55. data/lib/rspec/rails/matchers/action_cable.rb +65 -0
  56. data/lib/rspec/rails/matchers/action_mailbox.rb +64 -0
  57. data/lib/rspec/rails/matchers/active_job.rb +148 -22
  58. data/lib/rspec/rails/matchers/base_matcher.rb +5 -10
  59. data/lib/rspec/rails/matchers/have_enqueued_mail.rb +52 -28
  60. data/lib/rspec/rails/matchers/have_http_status.rb +11 -7
  61. data/lib/rspec/rails/matchers/have_rendered.rb +1 -0
  62. data/lib/rspec/rails/matchers/routing_matchers.rb +12 -12
  63. data/lib/rspec/rails/matchers.rb +10 -0
  64. data/lib/rspec/rails/tasks/rspec.rake +7 -17
  65. data/lib/rspec/rails/vendor/capybara.rb +10 -15
  66. data/lib/rspec/rails/version.rb +1 -1
  67. data/lib/rspec/rails/view_path_builder.rb +1 -1
  68. data/lib/rspec/rails/view_rendering.rb +15 -4
  69. data/lib/rspec-rails.rb +8 -9
  70. data.tar.gz.sig +0 -0
  71. metadata +46 -34
  72. metadata.gz.sig +0 -0
  73. data/lib/generators/rspec/observer/observer_generator.rb +0 -13
data/README.md CHANGED
@@ -8,6 +8,7 @@ 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` 3.x][]** for Rails earlier than 5.0.
11
12
  Use **[`rspec-rails` 1.x][]** for Rails 2.x.
12
13
 
13
14
  [Build Status]: https://secure.travis-ci.org/rspec/rspec-rails.svg?branch=master
@@ -19,6 +20,7 @@ Use **[`rspec-rails` 1.x][]** for Rails 2.x.
19
20
  [RSpec]: https://rspec.info/
20
21
  [Ruby on Rails]: https://rubyonrails.org/
21
22
  [`rspec-rails` 1.x]: https://github.com/dchelimsky/rspec-rails
23
+ [`rspec-rails` 3.x]: https://github.com/rspec/rspec-rails/tree/3-9-maintenance
22
24
 
23
25
  ## Installation
24
26
 
@@ -28,14 +30,14 @@ Use **[`rspec-rails` 1.x][]** for Rails 2.x.
28
30
  ```ruby
29
31
  # Run against the latest stable release
30
32
  group :development, :test do
31
- gem 'rspec-rails', '~> 3.8'
33
+ gem 'rspec-rails', '~> 4.0.0'
32
34
  end
33
35
 
34
36
  # Or, run against the master branch
35
37
  # (requires master-branch versions of all related RSpec libraries)
36
38
  group :development, :test do
37
39
  %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'
40
+ gem lib, git: "https://github.com/rspec/#{lib}.git", branch: 'master'
39
41
  end
40
42
  end
41
43
  ```
@@ -119,8 +121,7 @@ $ bundle exec rspec --help
119
121
  ```
120
122
 
121
123
  **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):
124
+ you can generate a binstub at `bin/rspec` and use that instead:
124
125
 
125
126
  ```sh
126
127
  $ bundle binstubs rspec-core
@@ -188,8 +189,8 @@ to test the various parts of a Rails system:
188
189
  | [`be_a_new`][] | | all | primarily intended for controller specs |
189
190
  | [`render_template`][] | `assert_template` | request / controller / view | use with `expect(response).to` |
190
191
  | [`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` |
192
+ | [`route_to`] | `assert_routing` | routing / controller | use with `expect(...).to route_to` |
193
+ | [`be_routable`] | | routing / controller | use with `expect(...).not_to be_routable` |
193
194
  | [`have_http_status`][] | | request / controller / feature | |
194
195
  | [`match_array`][] | | all | for comparing arrays of ActiveRecord objects |
195
196
  | [`have_been_enqueued`][] | | all | requires config: `ActiveJob::Base.queue_adapter = :test` |
@@ -362,7 +363,7 @@ you can run the specs and Cucumber features, or submit a pull request.
362
363
  ### Recommended third-party extensions
363
364
 
364
365
  * [FactoryBot](https://github.com/thoughtbot/factory_bot)
365
- * [Capybara](https://github.com/jnicklas/capybara)
366
+ * [Capybara](https://github.com/teamcapybara/capybara)
366
367
  (Included by default in Rails 5.1+.
367
368
  Note that [additional configuration is required][] to use the Capybara DSL
368
369
  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', File.join('spec/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,11 +4,20 @@ 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
+ File.join('spec/requests', class_path, "#{file_name}_request_spec.rb")
20
+ end
12
21
 
13
22
  def generate_controller_spec
14
23
  return unless options[:controller_specs]
@@ -29,6 +38,14 @@ module Rspec
29
38
  File.join("spec", "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
+ File.join('spec/routing', class_path, "#{file_name}_routing_spec.rb")
48
+ end
32
49
  end
33
50
  end
34
51
  end
@@ -0,0 +1,14 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe "<%= class_name.pluralize %>", <%= type_metatag(:request) %> do
4
+ <% namespaced_path = regular_class_path.join('/') %>
5
+ <% for action in actions -%>
6
+ describe "GET /<%= action %>" do
7
+ it "returns http success" do
8
+ get "<%= "/#{namespaced_path}" if namespaced_path != '' %>/<%= file_name %>/<%= action %>"
9
+ expect(response).to have_http_status(:success)
10
+ end
11
+ end
12
+
13
+ <% end -%>
14
+ 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,8 +4,8 @@ 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]
@@ -3,8 +3,8 @@ 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]
@@ -4,7 +4,7 @@ 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]
@@ -32,8 +32,8 @@ DESC
32
32
 
33
33
  def generate_rspec_init(tmpdir)
34
34
  initializer = ::RSpec::Core::ProjectInitializer.new(
35
- :destination => tmpdir,
36
- :report_stream => StringIO.new
35
+ destination: tmpdir,
36
+ report_stream: StringIO.new
37
37
  )
38
38
  initializer.run
39
39
 
@@ -47,7 +47,7 @@ DESC
47
47
  gsub_file spec_helper_path,
48
48
  'rspec --init',
49
49
  'rails generate rspec:install',
50
- :verbose => false
50
+ verbose: false
51
51
  end
52
52
 
53
53
  def remove_warnings_configuration(spec_helper_path)
@@ -56,7 +56,7 @@ DESC
56
56
  gsub_file spec_helper_path,
57
57
  /#{empty_line}(#{comment_line})+\s+config\.warnings = true\n/,
58
58
  '',
59
- :verbose => false
59
+ verbose: false
60
60
  end
61
61
  end
62
62
  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
4
  require File.expand_path('../config/environment', __dir__)
6
- <% else %>
7
- require File.expand_path('../../config/environment', __FILE__)
8
- <% end %>
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,9 +20,9 @@ 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
+ # Dir[Rails.root.join('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
@@ -35,15 +31,6 @@ rescue ActiveRecord::PendingMigrationError => e
35
31
  puts e.to_s.strip
36
32
  exit 1
37
33
  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
46
- end
47
34
  <% end -%>
48
35
  RSpec.configure do |config|
49
36
  <% if RSpec::Rails::FeatureCheck.has_active_record? -%>
@@ -55,6 +42,20 @@ RSpec.configure do |config|
55
42
  # instead of true.
56
43
  config.use_transactional_fixtures = true
57
44
 
45
+ # You can uncomment this line to turn off ActiveRecord support entirely.
46
+ # config.use_active_record = false
47
+
48
+ <% else -%>
49
+ # Remove this line to enable support for ActiveRecord
50
+ config.use_active_record = false
51
+
52
+ # If you enable ActiveRecord support you should unncomment these lines,
53
+ # note if you'd prefer not to run each example within a transaction, you
54
+ # should set use_transactional_fixtures to false.
55
+ #
56
+ # config.fixture_path = "#{::Rails.root}/spec/fixtures"
57
+ # config.use_transactional_fixtures = true
58
+
58
59
  <% end -%>
59
60
  # RSpec Rails can automatically mix in different behaviours to your tests
60
61
  # based on their file location, for example enabling you to call `get` and
@@ -63,7 +64,7 @@ RSpec.configure do |config|
63
64
  # You can disable this behaviour by removing the line below, and instead
64
65
  # explicitly tag your specs with their type, e.g.:
65
66
  #
66
- # RSpec.describe UsersController, :type => :controller do
67
+ # RSpec.describe UsersController, type: :controller do
67
68
  # # ...
68
69
  # end
69
70
  #
@@ -7,9 +7,9 @@ module Rspec
7
7
  # Add a deprecation for this class, before rspec-rails 4, to use the
8
8
  # `RequestGenerator` instead
9
9
  class_option :request_specs,
10
- :type => :boolean,
11
- :default => true,
12
- :desc => "Generate request specs"
10
+ type: :boolean,
11
+ default: true,
12
+ desc: "Generate request specs"
13
13
 
14
14
  def generate_request_spec
15
15
  return unless options[:request_specs]
@@ -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
+ File.join('spec/mailboxes', class_path, "#{file_name}_mailbox_spec.rb")
10
+ )
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ require 'rails_helper'
2
+
3
+ <% module_namespacing do -%>
4
+ RSpec.describe <%= class_name %>Mailbox, <%= type_metatag(:mailbox) %> do
5
+ pending "add some examples to (or delete) #{__FILE__}"
6
+ end
7
+ <% end -%>
@@ -5,7 +5,7 @@ module Rspec
5
5
  module Generators
6
6
  # @private
7
7
  class MailerGenerator < Base
8
- argument :actions, :type => :array, :default => [], :banner => "method method"
8
+ argument :actions, type: :array, default: [], banner: "method method"
9
9
 
10
10
  def generate_mailer_spec
11
11
  template "mailer_spec.rb", File.join('spec/mailers', class_path, "#{file_name}_spec.rb")
@@ -20,6 +20,7 @@ module Rspec
20
20
 
21
21
  def generate_preview_files
22
22
  return unless RSpec::Rails::FeatureCheck.has_action_mailer_preview?
23
+
23
24
  template "preview.rb", File.join("spec/mailers/previews", class_path, "#{file_name}_preview.rb")
24
25
  end
25
26
  end
@@ -5,10 +5,10 @@ module Rspec
5
5
  # @private
6
6
  class ModelGenerator < Base
7
7
  argument :attributes,
8
- :type => :array,
9
- :default => [],
10
- :banner => "field:type field:type"
11
- class_option :fixture, :type => :boolean
8
+ type: :array,
9
+ default: [],
10
+ banner: "field:type field:type"
11
+ class_option :fixture, type: :boolean
12
12
 
13
13
  def create_model_spec
14
14
  template_file = File.join(
@@ -23,6 +23,7 @@ module Rspec
23
23
 
24
24
  def create_fixture_file
25
25
  return unless missing_fixture_replacement?
26
+
26
27
  template 'fixtures.yml', File.join('spec/fixtures', class_path, "#{(pluralize_table_names? ? plural_file_name : file_name)}.yml")
27
28
  end
28
29
 
@@ -1,4 +1,4 @@
1
- # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
1
+ # Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2
2
 
3
3
  <% unless attributes.empty? -%>
4
4
  one:
@@ -4,7 +4,7 @@ module Rspec
4
4
  module Generators
5
5
  # @private
6
6
  class RequestGenerator < IntegrationGenerator
7
- source_paths << File.expand_path("../../integration/templates", __FILE__)
7
+ source_paths << File.expand_path('../integration/templates', __dir__)
8
8
  end
9
9
  end
10
10
  end
@@ -6,18 +6,19 @@ module Rspec
6
6
  # @private
7
7
  class ScaffoldGenerator < Base
8
8
  include ::Rails::Generators::ResourceHelpers
9
- source_paths << File.expand_path("../../helper/templates", __FILE__)
10
- argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
9
+ source_paths << File.expand_path('../helper/templates', __dir__)
10
+ argument :attributes, type: :array, default: [], banner: "field:type field:type"
11
11
 
12
- class_option :orm, :desc => "ORM used to generate the controller"
13
- class_option :template_engine, :desc => "Template engine to generate view files"
14
- class_option :singleton, :type => :boolean, :desc => "Supply to create a singleton controller"
15
- class_option :api, :type => :boolean, :desc => "Skip specs unnecessary for API-only apps"
12
+ class_option :orm, desc: "ORM used to generate the controller"
13
+ class_option :template_engine, desc: "Template engine to generate view files"
14
+ class_option :singleton, type: :boolean, desc: "Supply to create a singleton controller"
15
+ class_option :api, type: :boolean, desc: "Skip specs unnecessary for API-only apps"
16
16
 
17
- class_option :controller_specs, :type => :boolean, :default => true, :desc => "Generate controller specs"
18
- class_option :view_specs, :type => :boolean, :default => true, :desc => "Generate view specs"
19
- class_option :helper_specs, :type => :boolean, :default => true, :desc => "Generate helper specs"
20
- class_option :routing_specs, :type => :boolean, :default => true, :desc => "Generate routing specs"
17
+ class_option :controller_specs, type: :boolean, default: false, desc: "Generate controller specs"
18
+ class_option :request_specs, type: :boolean, default: true, desc: "Generate request specs"
19
+ class_option :view_specs, type: :boolean, default: true, desc: "Generate view specs"
20
+ class_option :helper_specs, type: :boolean, default: true, desc: "Generate helper specs"
21
+ class_option :routing_specs, type: :boolean, default: true, desc: "Generate routing specs"
21
22
 
22
23
  def initialize(*args, &blk)
23
24
  @generator_args = args.first
@@ -27,15 +28,20 @@ module Rspec
27
28
  def generate_controller_spec
28
29
  return unless options[:controller_specs]
29
30
 
30
- template_file = File.join(
31
- 'spec/controllers',
32
- controller_class_path,
33
- "#{controller_file_name}_controller_spec.rb"
34
- )
35
31
  if options[:api]
36
- template 'api_controller_spec.rb', template_file
32
+ template 'api_controller_spec.rb', template_file(folder: 'controllers', suffix: '_controller')
33
+ else
34
+ template 'controller_spec.rb', template_file(folder: 'controllers', suffix: '_controller')
35
+ end
36
+ end
37
+
38
+ def generate_request_spec
39
+ return unless options[:request_specs]
40
+
41
+ if options[:api]
42
+ template 'api_request_spec.rb', template_file(folder: 'requests')
37
43
  else
38
- template 'controller_spec.rb', template_file
44
+ template 'request_spec.rb', template_file(folder: 'requests')
39
45
  end
40
46
  end
41
47
 
@@ -60,8 +66,6 @@ module Rspec
60
66
  template 'routing_spec.rb', template_file
61
67
  end
62
68
 
63
- hook_for :integration_tool, :as => :integration
64
-
65
69
  protected
66
70
 
67
71
  attr_reader :generator_args
@@ -74,12 +78,14 @@ module Rspec
74
78
  # support for namespaced-resources
75
79
  def ns_file_name
76
80
  return file_name if ns_parts.empty?
81
+
77
82
  "#{ns_prefix.map(&:underscore).join('/')}_#{ns_suffix.singularize.underscore}"
78
83
  end
79
84
 
80
85
  # support for namespaced-resources
81
86
  def ns_table_name
82
87
  return table_name if ns_parts.empty?
88
+
83
89
  "#{ns_prefix.map(&:underscore).join('/')}/#{ns_suffix.tableize}"
84
90
  end
85
91
 
@@ -114,6 +120,10 @@ module Rspec
114
120
  end
115
121
  end
116
122
 
123
+ def template_file(folder:, suffix: '')
124
+ File.join('spec', folder, controller_class_path, "#{controller_file_name}#{suffix}_spec.rb")
125
+ end
126
+
117
127
  def banner
118
128
  self.class.banner
119
129
  end
@@ -46,11 +46,7 @@ RSpec.describe <%= controller_class_name %>Controller, <%= type_metatag(:control
46
46
  describe "GET #index" do
47
47
  it "returns a success response" do
48
48
  <%= file_name %> = <%= class_name %>.create! valid_attributes
49
- <% if RUBY_VERSION < '1.9.3' -%>
50
- get :index, {}, valid_session
51
- <% else -%>
52
49
  get :index, params: {}, session: valid_session
53
- <% end -%>
54
50
  expect(response).to be_successful
55
51
  end
56
52
  end
@@ -59,11 +55,7 @@ RSpec.describe <%= controller_class_name %>Controller, <%= type_metatag(:control
59
55
  describe "GET #show" do
60
56
  it "returns a success response" do
61
57
  <%= file_name %> = <%= class_name %>.create! valid_attributes
62
- <% if RUBY_VERSION < '1.9.3' -%>
63
- get :show, {:id => <%= file_name %>.to_param}, valid_session
64
- <% else -%>
65
58
  get :show, params: {id: <%= file_name %>.to_param}, session: valid_session
66
- <% end -%>
67
59
  expect(response).to be_successful
68
60
  end
69
61
  end
@@ -72,20 +64,12 @@ RSpec.describe <%= controller_class_name %>Controller, <%= type_metatag(:control
72
64
  context "with valid params" do
73
65
  it "creates a new <%= class_name %>" do
74
66
  expect {
75
- <% if RUBY_VERSION < '1.9.3' -%>
76
- post :create, {:<%= ns_file_name %> => valid_attributes}, valid_session
77
- <% else -%>
78
67
  post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session
79
- <% end -%>
80
68
  }.to change(<%= class_name %>, :count).by(1)
81
69
  end
82
70
 
83
71
  it "renders a JSON response with the new <%= ns_file_name %>" do
84
- <% if RUBY_VERSION < '1.9.3' -%>
85
- post :create, {:<%= ns_file_name %> => valid_attributes}, valid_session
86
- <% else %>
87
72
  post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session
88
- <% end -%>
89
73
  expect(response).to have_http_status(:created)
90
74
  expect(response.content_type).to eq('application/json')
91
75
  expect(response.location).to eq(<%= ns_file_name %>_url(<%= class_name %>.last))
@@ -94,11 +78,7 @@ RSpec.describe <%= controller_class_name %>Controller, <%= type_metatag(:control
94
78
 
95
79
  context "with invalid params" do
96
80
  it "renders a JSON response with errors for the new <%= ns_file_name %>" do
97
- <% if RUBY_VERSION < '1.9.3' -%>
98
- post :create, {:<%= ns_file_name %> => invalid_attributes}, valid_session
99
- <% else %>
100
81
  post :create, params: {<%= ns_file_name %>: invalid_attributes}, session: valid_session
101
- <% end -%>
102
82
  expect(response).to have_http_status(:unprocessable_entity)
103
83
  expect(response.content_type).to eq('application/json')
104
84
  end
@@ -113,22 +93,14 @@ RSpec.describe <%= controller_class_name %>Controller, <%= type_metatag(:control
113
93
 
114
94
  it "updates the requested <%= ns_file_name %>" do
115
95
  <%= file_name %> = <%= class_name %>.create! valid_attributes
116
- <% if RUBY_VERSION < '1.9.3' -%>
117
- put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => new_attributes}, valid_session
118
- <% else -%>
119
96
  put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: new_attributes}, session: valid_session
120
- <% end -%>
121
97
  <%= file_name %>.reload
122
98
  skip("Add assertions for updated state")
123
99
  end
124
100
 
125
101
  it "renders a JSON response with the <%= ns_file_name %>" do
126
102
  <%= file_name %> = <%= class_name %>.create! valid_attributes
127
- <% if RUBY_VERSION < '1.9.3' -%>
128
- put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => valid_attributes}, valid_session
129
- <% else %>
130
103
  put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: valid_attributes}, session: valid_session
131
- <% end -%>
132
104
  expect(response).to have_http_status(:ok)
133
105
  expect(response.content_type).to eq('application/json')
134
106
  end
@@ -137,11 +109,7 @@ RSpec.describe <%= controller_class_name %>Controller, <%= type_metatag(:control
137
109
  context "with invalid params" do
138
110
  it "renders a JSON response with errors for the <%= ns_file_name %>" do
139
111
  <%= file_name %> = <%= class_name %>.create! valid_attributes
140
- <% if RUBY_VERSION < '1.9.3' -%>
141
- put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => invalid_attributes}, valid_session
142
- <% else %>
143
112
  put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: invalid_attributes}, session: valid_session
144
- <% end -%>
145
113
  expect(response).to have_http_status(:unprocessable_entity)
146
114
  expect(response.content_type).to eq('application/json')
147
115
  end
@@ -152,11 +120,7 @@ RSpec.describe <%= controller_class_name %>Controller, <%= type_metatag(:control
152
120
  it "destroys the requested <%= ns_file_name %>" do
153
121
  <%= file_name %> = <%= class_name %>.create! valid_attributes
154
122
  expect {
155
- <% if RUBY_VERSION < '1.9.3' -%>
156
- delete :destroy, {:id => <%= file_name %>.to_param}, valid_session
157
- <% else -%>
158
123
  delete :destroy, params: {id: <%= file_name %>.to_param}, session: valid_session
159
- <% end -%>
160
124
  }.to change(<%= class_name %>, :count).by(-1)
161
125
  end
162
126
  end