rspec-rails 3.8.2 → 5.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/Capybara.md +5 -54
  4. data/Changelog.md +284 -74
  5. data/README.md +278 -500
  6. data/lib/generators/rspec/channel/channel_generator.rb +12 -0
  7. data/lib/generators/rspec/{observer/templates/observer_spec.rb → channel/templates/channel_spec.rb.erb} +1 -1
  8. data/lib/generators/rspec/controller/controller_generator.rb +22 -5
  9. data/lib/generators/rspec/controller/templates/request_spec.rb +19 -0
  10. data/lib/generators/rspec/controller/templates/routing_spec.rb +13 -0
  11. data/lib/generators/rspec/feature/feature_generator.rb +2 -2
  12. data/lib/generators/rspec/generator/generator_generator.rb +24 -0
  13. data/lib/generators/rspec/generator/templates/generator_spec.rb +6 -0
  14. data/lib/generators/rspec/helper/helper_generator.rb +1 -1
  15. data/lib/generators/rspec/install/install_generator.rb +4 -4
  16. data/lib/generators/rspec/install/templates/spec/rails_helper.rb +18 -13
  17. data/lib/generators/rspec/integration/integration_generator.rb +3 -3
  18. data/lib/generators/rspec/job/job_generator.rb +2 -1
  19. data/lib/generators/rspec/job/templates/job_spec.rb.erb +1 -1
  20. data/lib/generators/rspec/mailbox/mailbox_generator.rb +14 -0
  21. data/lib/generators/rspec/mailbox/templates/mailbox_spec.rb.erb +7 -0
  22. data/lib/generators/rspec/mailer/mailer_generator.rb +2 -1
  23. data/lib/generators/rspec/mailer/templates/mailer_spec.rb +2 -2
  24. data/lib/generators/rspec/mailer/templates/preview.rb +1 -1
  25. data/lib/generators/rspec/model/model_generator.rb +6 -5
  26. data/lib/generators/rspec/model/templates/fixtures.yml +1 -1
  27. data/lib/generators/rspec/request/request_generator.rb +1 -1
  28. data/lib/generators/rspec/scaffold/scaffold_generator.rb +33 -19
  29. data/lib/generators/rspec/scaffold/templates/api_controller_spec.rb +13 -49
  30. data/lib/generators/rspec/scaffold/templates/api_request_spec.rb +131 -0
  31. data/lib/generators/rspec/scaffold/templates/controller_spec.rb +10 -58
  32. data/lib/generators/rspec/scaffold/templates/edit_spec.rb +1 -5
  33. data/lib/generators/rspec/scaffold/templates/index_spec.rb +2 -2
  34. data/lib/generators/rspec/scaffold/templates/new_spec.rb +1 -5
  35. data/lib/generators/rspec/scaffold/templates/request_spec.rb +138 -0
  36. data/lib/generators/rspec/scaffold/templates/routing_spec.rb +8 -10
  37. data/lib/generators/rspec/scaffold/templates/show_spec.rb +1 -1
  38. data/lib/generators/rspec/system/system_generator.rb +24 -0
  39. data/lib/generators/rspec/system/templates/system_spec.rb +9 -0
  40. data/lib/generators/rspec/view/view_generator.rb +2 -2
  41. data/lib/generators/rspec.rb +0 -6
  42. data/lib/rspec/rails/adapters.rb +11 -76
  43. data/lib/rspec/rails/configuration.rb +82 -37
  44. data/lib/rspec/rails/example/channel_example_group.rb +93 -0
  45. data/lib/rspec/rails/example/controller_example_group.rb +5 -4
  46. data/lib/rspec/rails/example/feature_example_group.rb +6 -26
  47. data/lib/rspec/rails/example/helper_example_group.rb +2 -9
  48. data/lib/rspec/rails/example/mailbox_example_group.rb +80 -0
  49. data/lib/rspec/rails/example/mailer_example_group.rb +2 -2
  50. data/lib/rspec/rails/example/rails_example_group.rb +1 -1
  51. data/lib/rspec/rails/example/request_example_group.rb +1 -4
  52. data/lib/rspec/rails/example/system_example_group.rb +29 -12
  53. data/lib/rspec/rails/example/view_example_group.rb +38 -27
  54. data/lib/rspec/rails/example.rb +2 -0
  55. data/lib/rspec/rails/extensions/active_record/proxy.rb +5 -10
  56. data/lib/rspec/rails/feature_check.rb +12 -29
  57. data/lib/rspec/rails/file_fixture_support.rb +9 -11
  58. data/lib/rspec/rails/fixture_file_upload_support.rb +33 -17
  59. data/lib/rspec/rails/fixture_support.rb +35 -32
  60. data/lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb +170 -0
  61. data/lib/rspec/rails/matchers/action_cable/have_streams.rb +58 -0
  62. data/lib/rspec/rails/matchers/action_cable.rb +65 -0
  63. data/lib/rspec/rails/matchers/action_mailbox.rb +73 -0
  64. data/lib/rspec/rails/matchers/active_job.rb +170 -22
  65. data/lib/rspec/rails/matchers/base_matcher.rb +179 -0
  66. data/lib/rspec/rails/matchers/be_a_new.rb +1 -1
  67. data/lib/rspec/rails/matchers/be_new_record.rb +1 -1
  68. data/lib/rspec/rails/matchers/be_valid.rb +1 -1
  69. data/lib/rspec/rails/matchers/have_enqueued_mail.rb +226 -0
  70. data/lib/rspec/rails/matchers/have_http_status.rb +20 -25
  71. data/lib/rspec/rails/matchers/have_rendered.rb +2 -1
  72. data/lib/rspec/rails/matchers/redirect_to.rb +1 -1
  73. data/lib/rspec/rails/matchers/relation_match_array.rb +1 -1
  74. data/lib/rspec/rails/matchers/routing_matchers.rb +14 -14
  75. data/lib/rspec/rails/matchers.rb +11 -0
  76. data/lib/rspec/rails/tasks/rspec.rake +7 -17
  77. data/lib/rspec/rails/vendor/capybara.rb +10 -15
  78. data/lib/rspec/rails/version.rb +1 -1
  79. data/lib/rspec/rails/view_path_builder.rb +1 -1
  80. data/lib/rspec/rails/view_rendering.rb +15 -4
  81. data/lib/rspec-rails.rb +13 -16
  82. data.tar.gz.sig +0 -0
  83. metadata +64 -34
  84. metadata.gz.sig +0 -0
  85. data/lib/generators/rspec/observer/observer_generator.rb +0 -13
@@ -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}_spec.rb")
20
+ end
12
21
 
13
22
  def generate_controller_spec
14
23
  return unless options[:controller_specs]
@@ -18,7 +27,7 @@ module Rspec
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)
@@ -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,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,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]
@@ -0,0 +1,24 @@
1
+ require 'generators/rspec'
2
+
3
+ module Rspec
4
+ module Generators
5
+ # @private
6
+ class GeneratorGenerator < Base
7
+ class_option :generator_specs, type: :boolean, default: true, desc: "Generate generator specs"
8
+
9
+ def generate_generator_spec
10
+ return unless options[:generator_specs]
11
+
12
+ template template_name, File.join('spec/generator', class_path, filename)
13
+ end
14
+
15
+ def template_name
16
+ 'generator_spec.rb'
17
+ end
18
+
19
+ def filename
20
+ "#{table_name}_generator_spec.rb"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,6 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe "<%= class_name.pluralize %>", <%= type_metatag(:generator) %> do
4
+
5
+ pending "add some scenarios (or delete) #{__FILE__}"
6
+ end
@@ -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,7 +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
- require File.expand_path('../../config/environment', __FILE__)
4
+ require_relative '../config/environment'
5
5
  # Prevent database truncation if the environment is production
6
6
  abort("The Rails environment is running in production mode!") if Rails.env.production?
7
7
  require 'rspec/rails'
@@ -20,9 +20,9 @@ require 'rspec/rails'
20
20
  # directory. Alternatively, in the individual `*_spec.rb` files, manually
21
21
  # require only the support files necessary.
22
22
  #
23
- # Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
23
+ # Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
24
24
 
25
- <% if RSpec::Rails::FeatureCheck.can_maintain_test_schema? -%>
25
+ <% if RSpec::Rails::FeatureCheck.has_active_record_migration? -%>
26
26
  # Checks for pending migrations and applies them before tests are run.
27
27
  # If you are not using ActiveRecord, you can remove these lines.
28
28
  begin
@@ -31,15 +31,6 @@ rescue ActiveRecord::PendingMigrationError => e
31
31
  puts e.to_s.strip
32
32
  exit 1
33
33
  end
34
- <% elsif RSpec::Rails::FeatureCheck.can_check_pending_migrations? -%>
35
- # Checks for pending migrations before tests are run.
36
- # If you are not using ActiveRecord, you can remove these lines.
37
- begin
38
- ActiveRecord::Migration.check_pending!
39
- rescue ActiveRecord::PendingMigrationError => e
40
- puts e.to_s.strip
41
- exit 1
42
- end
43
34
  <% end -%>
44
35
  RSpec.configure do |config|
45
36
  <% if RSpec::Rails::FeatureCheck.has_active_record? -%>
@@ -51,6 +42,20 @@ RSpec.configure do |config|
51
42
  # instead of true.
52
43
  config.use_transactional_fixtures = true
53
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
+
54
59
  <% end -%>
55
60
  # RSpec Rails can automatically mix in different behaviours to your tests
56
61
  # based on their file location, for example enabling you to call `get` and
@@ -59,7 +64,7 @@ RSpec.configure do |config|
59
64
  # You can disable this behaviour by removing the line below, and instead
60
65
  # explicitly tag your specs with their type, e.g.:
61
66
  #
62
- # RSpec.describe UsersController, :type => :controller do
67
+ # RSpec.describe UsersController, type: :controller do
63
68
  # # ...
64
69
  # end
65
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]
@@ -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', File.join('spec/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
+ 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
@@ -1,10 +1,10 @@
1
1
  require "rails_helper"
2
2
 
3
3
  <% module_namespacing do -%>
4
- RSpec.describe <%= Rails.version.to_f >= 5.0 ? class_name.sub(/(Mailer)?$/, 'Mailer') : class_name %>, <%= type_metatag(:mailer) %> do
4
+ RSpec.describe <%= class_name.sub(/(Mailer)?$/, 'Mailer') %>, <%= type_metatag(:mailer) %> do
5
5
  <% for action in actions -%>
6
6
  describe "<%= action %>" do
7
- let(:mail) { <%= Rails.version.to_f >= 5.0 ? class_name.sub(/(Mailer)?$/, 'Mailer') : class_name %>.<%= action %> }
7
+ let(:mail) { <%= class_name.sub(/(Mailer)?$/, 'Mailer') %>.<%= action %> }
8
8
 
9
9
  it "renders the headers" do
10
10
  expect(mail.subject).to eq(<%= action.to_s.humanize.inspect %>)
@@ -5,7 +5,7 @@ class <%= class_name %>Preview < ActionMailer::Preview
5
5
 
6
6
  # Preview this email at http://localhost:3000/rails/mailers/<%= file_path %>/<%= action %>
7
7
  def <%= action %>
8
- <%= Rails.version.to_f >= 5.0 ? class_name.sub(/(Mailer)?$/, 'Mailer') : class_name %>.<%= action %>
8
+ <%= class_name.sub(/(Mailer)?$/, 'Mailer') %>.<%= action %>
9
9
  end
10
10
  <% end -%>
11
11
 
@@ -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,7 +23,8 @@ module Rspec
23
23
 
24
24
  def create_fixture_file
25
25
  return unless missing_fixture_replacement?
26
- template 'fixtures.yml', File.join('spec/fixtures', "#{table_name}.yml")
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
 
29
30
  private
@@ -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,9 +120,17 @@ 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
130
+
131
+ def show_helper(resource_name = file_name)
132
+ "#{singular_route_name}_url(#{resource_name})"
133
+ end
120
134
  end
121
135
  end
122
136
  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,33 +64,21 @@ 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
- post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session
79
- <% end -%>
67
+ post :create, params: {<%= singular_table_name %>: valid_attributes}, session: valid_session
80
68
  }.to change(<%= class_name %>, :count).by(1)
81
69
  end
82
70
 
83
- 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
- post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session
88
- <% end -%>
71
+ it "renders a JSON response with the new <%= singular_table_name %>" do
72
+ post :create, params: {<%= singular_table_name %>: valid_attributes}, session: valid_session
89
73
  expect(response).to have_http_status(:created)
90
74
  expect(response.content_type).to eq('application/json')
91
- expect(response.location).to eq(<%= ns_file_name %>_url(<%= class_name %>.last))
75
+ expect(response.location).to eq(<%= singular_table_name %>_url(<%= class_name %>.last))
92
76
  end
93
77
  end
94
78
 
95
79
  context "with invalid params" do
96
- 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
- post :create, params: {<%= ns_file_name %>: invalid_attributes}, session: valid_session
101
- <% end -%>
80
+ it "renders a JSON response with errors for the new <%= singular_table_name %>" do
81
+ post :create, params: {<%= singular_table_name %>: invalid_attributes}, session: valid_session
102
82
  expect(response).to have_http_status(:unprocessable_entity)
103
83
  expect(response.content_type).to eq('application/json')
104
84
  end
@@ -111,37 +91,25 @@ RSpec.describe <%= controller_class_name %>Controller, <%= type_metatag(:control
111
91
  skip("Add a hash of attributes valid for your model")
112
92
  }
113
93
 
114
- it "updates the requested <%= ns_file_name %>" do
94
+ it "updates the requested <%= singular_table_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
- put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: new_attributes}, session: valid_session
120
- <% end -%>
96
+ put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: new_attributes}, session: valid_session
121
97
  <%= file_name %>.reload
122
98
  skip("Add assertions for updated state")
123
99
  end
124
100
 
125
- it "renders a JSON response with the <%= ns_file_name %>" do
101
+ it "renders a JSON response with the <%= singular_table_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
- put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: valid_attributes}, session: valid_session
131
- <% end -%>
103
+ put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: new_attributes}, session: valid_session
132
104
  expect(response).to have_http_status(:ok)
133
105
  expect(response.content_type).to eq('application/json')
134
106
  end
135
107
  end
136
108
 
137
109
  context "with invalid params" do
138
- it "renders a JSON response with errors for the <%= ns_file_name %>" do
110
+ it "renders a JSON response with errors for the <%= singular_table_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
- put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: invalid_attributes}, session: valid_session
144
- <% end -%>
112
+ put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: invalid_attributes}, session: valid_session
145
113
  expect(response).to have_http_status(:unprocessable_entity)
146
114
  expect(response.content_type).to eq('application/json')
147
115
  end
@@ -149,14 +117,10 @@ RSpec.describe <%= controller_class_name %>Controller, <%= type_metatag(:control
149
117
  end
150
118
 
151
119
  describe "DELETE #destroy" do
152
- it "destroys the requested <%= ns_file_name %>" do
120
+ it "destroys the requested <%= singular_table_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