rspec-rails 3.8.2 → 6.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/Capybara.md +5 -54
  4. data/Changelog.md +339 -75
  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 +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/generator/generator_generator.rb +24 -0
  13. data/lib/generators/rspec/generator/templates/generator_spec.rb +6 -0
  14. data/lib/generators/rspec/helper/helper_generator.rb +2 -2
  15. data/lib/generators/rspec/install/install_generator.rb +23 -6
  16. data/lib/generators/rspec/install/templates/spec/rails_helper.rb +20 -16
  17. data/lib/generators/rspec/integration/integration_generator.rb +13 -6
  18. data/lib/generators/rspec/job/job_generator.rb +2 -1
  19. data/lib/generators/rspec/job/templates/job_spec.rb.erb +1 -1
  20. data/lib/generators/rspec/mailbox/mailbox_generator.rb +14 -0
  21. data/lib/generators/rspec/mailbox/templates/mailbox_spec.rb.erb +7 -0
  22. data/lib/generators/rspec/mailer/mailer_generator.rb +5 -4
  23. data/lib/generators/rspec/mailer/templates/mailer_spec.rb +2 -2
  24. data/lib/generators/rspec/mailer/templates/preview.rb +1 -1
  25. data/lib/generators/rspec/model/model_generator.rb +8 -7
  26. data/lib/generators/rspec/model/templates/fixtures.yml +1 -1
  27. data/lib/generators/rspec/request/request_generator.rb +10 -3
  28. data/lib/generators/rspec/scaffold/scaffold_generator.rb +36 -22
  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 +25 -58
  32. data/lib/generators/rspec/scaffold/templates/edit_spec.rb +9 -9
  33. data/lib/generators/rspec/scaffold/templates/index_spec.rb +3 -2
  34. data/lib/generators/rspec/scaffold/templates/new_spec.rb +1 -5
  35. data/lib/generators/rspec/scaffold/templates/request_spec.rb +153 -0
  36. data/lib/generators/rspec/scaffold/templates/routing_spec.rb +8 -10
  37. data/lib/generators/rspec/scaffold/templates/show_spec.rb +2 -2
  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 +4 -4
  41. data/lib/generators/rspec.rb +16 -5
  42. data/lib/rspec/rails/adapters.rb +22 -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 +2 -1
  51. data/lib/rspec/rails/example/request_example_group.rb +1 -4
  52. data/lib/rspec/rails/example/system_example_group.rb +34 -16
  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 +16 -29
  57. data/lib/rspec/rails/file_fixture_support.rb +11 -10
  58. data/lib/rspec/rails/fixture_file_upload_support.rb +14 -15
  59. data/lib/rspec/rails/fixture_support.rb +40 -32
  60. data/lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb +173 -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 +173 -25
  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 +227 -0
  70. data/lib/rspec/rails/matchers/have_http_status.rb +21 -26
  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 +13 -13
  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 -17
  78. data/lib/rspec/rails/version.rb +1 -1
  79. data/lib/rspec/rails/view_assigns.rb +0 -18
  80. data/lib/rspec/rails/view_path_builder.rb +1 -1
  81. data/lib/rspec/rails/view_rendering.rb +20 -7
  82. data/lib/rspec-rails.rb +10 -17
  83. data.tar.gz.sig +0 -0
  84. metadata +51 -33
  85. metadata.gz.sig +0 -0
  86. data/lib/generators/rspec/observer/observer_generator.rb +0 -13
  87. /data/lib/generators/rspec/{integration → request}/templates/request_spec.rb +0 -0
@@ -0,0 +1,80 @@
1
+ module RSpec
2
+ module Rails
3
+ # @api public
4
+ # Container module for mailbox spec functionality.
5
+ module MailboxExampleGroup
6
+ extend ActiveSupport::Concern
7
+
8
+ if RSpec::Rails::FeatureCheck.has_action_mailbox?
9
+ require 'action_mailbox/test_helper'
10
+ extend ::ActionMailbox::TestHelper
11
+
12
+ # @private
13
+ def self.create_inbound_email(arg)
14
+ case arg
15
+ when Hash
16
+ create_inbound_email_from_mail(**arg)
17
+ else
18
+ create_inbound_email_from_source(arg.to_s)
19
+ end
20
+ end
21
+ else
22
+ def self.create_inbound_email(_arg)
23
+ raise "Could not load ActionMailer::TestHelper"
24
+ end
25
+ end
26
+
27
+ class_methods do
28
+ # @private
29
+ def mailbox_class
30
+ described_class
31
+ end
32
+ end
33
+
34
+ included do
35
+ subject { described_class }
36
+ end
37
+
38
+ # @api public
39
+ # Passes if the inbound email was delivered
40
+ #
41
+ # @example
42
+ # inbound_email = process(args)
43
+ # expect(inbound_email).to have_been_delivered
44
+ def have_been_delivered
45
+ satisfy('have been delivered', &:delivered?)
46
+ end
47
+
48
+ # @api public
49
+ # Passes if the inbound email bounced during processing
50
+ #
51
+ # @example
52
+ # inbound_email = process(args)
53
+ # expect(inbound_email).to have_bounced
54
+ def have_bounced
55
+ satisfy('have bounced', &:bounced?)
56
+ end
57
+
58
+ # @api public
59
+ # Passes if the inbound email failed to process
60
+ #
61
+ # @example
62
+ # inbound_email = process(args)
63
+ # expect(inbound_email).to have_failed
64
+ def have_failed
65
+ satisfy('have failed', &:failed?)
66
+ end
67
+
68
+ # Process an inbound email message directly, bypassing routing.
69
+ #
70
+ # @param message [Hash, Mail::Message] a mail message or hash of
71
+ # attributes used to build one
72
+ # @return [ActionMaibox::InboundMessage]
73
+ def process(message)
74
+ MailboxExampleGroup.create_inbound_email(message).tap do |mail|
75
+ self.class.mailbox_class.receive(mail)
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -21,8 +21,8 @@ if defined?(ActionMailer)
21
21
 
22
22
  included do
23
23
  include ::Rails.application.routes.url_helpers
24
- options = ::Rails.configuration.action_mailer.default_url_options
25
- options.each { |key, value| default_url_options[key] = value } if options
24
+ options = ::Rails.configuration.action_mailer.default_url_options || {}
25
+ options.each { |key, value| default_url_options[key] = value }
26
26
  end
27
27
 
28
28
  # Class-level DSL for mailer specs.
@@ -9,9 +9,10 @@ module RSpec
9
9
  module RailsExampleGroup
10
10
  extend ActiveSupport::Concern
11
11
  include RSpec::Rails::SetupAndTeardownAdapter
12
- include RSpec::Rails::MinitestLifecycleAdapter if ::Rails::VERSION::STRING >= '4'
12
+ include RSpec::Rails::MinitestLifecycleAdapter
13
13
  include RSpec::Rails::MinitestAssertionAdapter
14
14
  include RSpec::Rails::FixtureSupport
15
+ include RSpec::Rails::TaggedLoggingAdapter if ::Rails::VERSION::MAJOR >= 7
15
16
  end
16
17
  end
17
18
  end
@@ -10,10 +10,7 @@ module RSpec
10
10
  include RSpec::Rails::Matchers::RedirectTo
11
11
  include RSpec::Rails::Matchers::RenderTemplate
12
12
  include ActionController::TemplateAssertions
13
-
14
- if ActionPack::VERSION::MAJOR >= 5
15
- include ActionDispatch::IntegrationTest::Behavior
16
- end
13
+ include ActionDispatch::IntegrationTest::Behavior
17
14
 
18
15
  # Delegates to `Rails.application`.
19
16
  def app
@@ -15,7 +15,11 @@ module RSpec
15
15
  CHARS_TO_TRANSLATE = ['/', '.', ':', ',', "'", '"', " "].freeze
16
16
 
17
17
  # @private
18
- module BlowAwayAfterTeardownHook
18
+ module BlowAwayTeardownHooks
19
+ # @private
20
+ def before_teardown
21
+ end
22
+
19
23
  # @private
20
24
  def after_teardown
21
25
  end
@@ -37,7 +41,7 @@ module RSpec
37
41
  @method_name ||= [
38
42
  self.class.name.underscore,
39
43
  RSpec.current_example.description.underscore
40
- ].join("_").tr(CHARS_TO_TRANSLATE.join, "_")[0...200] + "_#{rand(1000)}"
44
+ ].join("_").tr(CHARS_TO_TRANSLATE.join, "_").byteslice(0...200).scrub("") + "_#{rand(1000)}"
41
45
  end
42
46
 
43
47
  # Delegates to `Rails.application`.
@@ -46,28 +50,33 @@ module RSpec
46
50
  end
47
51
 
48
52
  included do |other|
53
+ ActiveSupport.on_load(:action_dispatch_system_test_case) do
54
+ ActionDispatch::SystemTesting::Server.silence_puma = true
55
+ end
56
+
57
+ require 'action_dispatch/system_test_case'
58
+
49
59
  begin
50
60
  require 'capybara'
51
- require 'action_dispatch/system_test_case'
52
- # rubocop:disable Lint/HandleExceptions
53
61
  rescue LoadError => e
54
- # rubocop:enable Lint/HandleExceptions
55
62
  abort """
56
63
  LoadError: #{e.message}
57
- System test integration requires Rails >= 5.1 and has a hard
64
+ System test integration has a hard
58
65
  dependency on a webserver and `capybara`, please add capybara to
59
66
  your Gemfile and configure a webserver (e.g. `Capybara.server =
60
- :webrick`) before attempting to use system specs.
67
+ :puma`) before attempting to use system specs.
61
68
  """.gsub(/\s+/, ' ').strip
62
69
  end
63
70
 
71
+ original_before_teardown =
72
+ ::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown.instance_method(:before_teardown)
73
+
64
74
  original_after_teardown =
65
75
  ::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown.instance_method(:after_teardown)
66
76
 
67
- other.include ActionDispatch::IntegrationTest::Behavior
68
77
  other.include ::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown
69
78
  other.include ::ActionDispatch::SystemTesting::TestHelpers::ScreenshotHelper
70
- other.include BlowAwayAfterTeardownHook
79
+ other.include BlowAwayTeardownHooks
71
80
 
72
81
  attr_reader :driver
73
82
 
@@ -78,16 +87,19 @@ module RSpec
78
87
  def initialize(*args, &blk)
79
88
  super(*args, &blk)
80
89
  @driver = nil
90
+
91
+ self.class.before do
92
+ # A user may have already set the driver, so only default if driver
93
+ # is not set
94
+ driven_by(:selenium) unless @driver
95
+ end
81
96
  end
82
97
 
83
- def driven_by(*args, &blk)
84
- @driver = ::ActionDispatch::SystemTestCase.driven_by(*args, &blk).tap(&:use)
98
+ def driven_by(driver, **driver_options, &blk)
99
+ @driver = ::ActionDispatch::SystemTestCase.driven_by(driver, **driver_options, &blk).tap(&:use)
85
100
  end
86
101
 
87
102
  before do
88
- # A user may have already set the driver, so only default if driver
89
- # is not set
90
- driven_by(:selenium) unless @driver
91
103
  @routes = ::Rails.application.routes
92
104
  end
93
105
 
@@ -95,13 +107,19 @@ module RSpec
95
107
  orig_stdout = $stdout
96
108
  $stdout = StringIO.new
97
109
  begin
98
- original_after_teardown.bind(self).call
110
+ original_before_teardown.bind(self).call
99
111
  ensure
100
112
  myio = $stdout
101
- RSpec.current_example.metadata[:extra_failure_lines] = myio.string
113
+ myio.rewind
114
+ RSpec.current_example.metadata[:extra_failure_lines] = myio.readlines
102
115
  $stdout = orig_stdout
103
116
  end
104
117
  end
118
+
119
+ around do |example|
120
+ example.run
121
+ original_after_teardown.bind(self).call
122
+ end
105
123
  end
106
124
  end
107
125
  end
@@ -43,16 +43,13 @@ module RSpec
43
43
 
44
44
  included do
45
45
  include ::Rails.application.routes.url_helpers
46
-
47
- if ::Rails.application.routes.respond_to?(:mounted_helpers)
48
- include ::Rails.application.routes.mounted_helpers
49
- end
46
+ include ::Rails.application.routes.mounted_helpers
50
47
  end
51
48
 
52
49
  # @overload render
53
- # @overload render({:partial => path_to_file})
54
- # @overload render({:partial => path_to_file}, {... locals ...})
55
- # @overload render({:partial => path_to_file}, {... locals ...}) do ... end
50
+ # @overload render({partial: path_to_file})
51
+ # @overload render({partial: path_to_file}, {... locals ...})
52
+ # @overload render({partial: path_to_file}, {... locals ...}) do ... end
56
53
  #
57
54
  # Delegates to ActionView::Base#render, so see documentation on that
58
55
  # for more info.
@@ -62,7 +59,7 @@ module RSpec
62
59
  #
63
60
  # describe "widgets/new.html.erb" do
64
61
  # it "shows all the widgets" do
65
- # render # => view.render(:file => "widgets/new.html.erb")
62
+ # render # => view.render(file: "widgets/new.html.erb")
66
63
  # # ...
67
64
  # end
68
65
  # end
@@ -105,7 +102,7 @@ module RSpec
105
102
 
106
103
  # @deprecated Use `view` instead.
107
104
  def template
108
- RSpec.deprecate("template", :replacement => "view")
105
+ RSpec.deprecate("template", replacement: "view")
109
106
  view
110
107
  end
111
108
 
@@ -128,20 +125,37 @@ module RSpec
128
125
  private
129
126
 
130
127
  def _default_render_options
131
- if ::Rails::VERSION::STRING >= '3.2'
132
- # pluck the handler, format, and locale out of, eg, posts/index.de.html.haml
133
- template, *components = _default_file_to_render.split('.')
134
- handler, format, locale = *components.reverse
135
-
136
- render_options = { :template => template }
137
- render_options[:handlers] = [handler] if handler
138
- render_options[:formats] = [format.to_sym] if format
139
- render_options[:locales] = [locale] if locale
140
-
141
- render_options
142
- else
143
- { :template => _default_file_to_render }
144
- end
128
+ formats = if ActionView::Template::Types.respond_to?(:symbols)
129
+ ActionView::Template::Types.symbols
130
+ else
131
+ [:html, :text, :js, :css, :xml, :json].map(&:to_s)
132
+ end.map { |x| Regexp.escape(x) }.join("|")
133
+
134
+ handlers = ActionView::Template::Handlers.extensions.map { |x| Regexp.escape(x) }.join("|")
135
+ locales = "[a-z]{2}(?:-[A-Z]{2})?"
136
+ variants = "[^.]*"
137
+ path_regex = %r{
138
+ \A
139
+ (?<template>.*?)
140
+ (?:\.(?<locale>#{locales}))??
141
+ (?:\.(?<format>#{formats}))??
142
+ (?:\+(?<variant>#{variants}))??
143
+ (?:\.(?<handler>#{handlers}))?
144
+ \z
145
+ }x
146
+
147
+ # This regex should always find a match.
148
+ # Worst case, everything will be nil, and :template will just be
149
+ # the original string.
150
+ match = path_regex.match(_default_file_to_render)
151
+
152
+ render_options = { template: match[:template] }
153
+ render_options[:handlers] = [match[:handler].to_sym] if match[:handler]
154
+ render_options[:formats] = [match[:format].to_sym] if match[:format]
155
+ render_options[:locales] = [match[:locale].to_sym] if match[:locale]
156
+ render_options[:variants] = [match[:variant].to_sym] if match[:variant]
157
+
158
+ render_options
145
159
  end
146
160
 
147
161
  def _path_parts
@@ -171,10 +185,7 @@ module RSpec
171
185
 
172
186
  before do
173
187
  _include_controller_helpers
174
- if view.lookup_context.respond_to?(:prefixes)
175
- # rails 3.1
176
- view.lookup_context.prefixes << _controller_path
177
- end
188
+ view.lookup_context.prefixes << _controller_path
178
189
 
179
190
  controller.controller_path = _controller_path
180
191
 
@@ -9,3 +9,5 @@ require 'rspec/rails/example/model_example_group'
9
9
  require 'rspec/rails/example/job_example_group'
10
10
  require 'rspec/rails/example/feature_example_group'
11
11
  require 'rspec/rails/example/system_example_group'
12
+ require 'rspec/rails/example/channel_example_group'
13
+ require 'rspec/rails/example/mailbox_example_group'
@@ -1,16 +1,11 @@
1
1
  RSpec.configure do |rspec|
2
2
  # Delay this in order to give users a chance to configure `expect_with`...
3
3
  rspec.before(:suite) do
4
- if defined?(RSpec::Matchers) && RSpec::Matchers.configuration.syntax.include?(:should) && defined?(ActiveRecord::Associations)
5
- # In Rails 3.0, it was AssociationProxy.
6
- # In 3.1+, it's CollectionProxy.
7
- const_name = [:CollectionProxy, :AssociationProxy].find do |const|
8
- ActiveRecord::Associations.const_defined?(const)
9
- end
10
-
11
- proxy_class = ActiveRecord::Associations.const_get(const_name)
12
-
13
- RSpec::Matchers.configuration.add_should_and_should_not_to proxy_class
4
+ if defined?(RSpec::Matchers) &&
5
+ RSpec::Matchers.configuration.respond_to?(:syntax) && # RSpec 4 dropped support for monkey-patching `should` syntax
6
+ RSpec::Matchers.configuration.syntax.include?(:should) &&
7
+ defined?(ActiveRecord::Associations)
8
+ RSpec::Matchers.configuration.add_should_and_should_not_to ActiveRecord::Associations::CollectionProxy
14
9
  end
15
10
  end
16
11
  end
@@ -1,23 +1,8 @@
1
1
  module RSpec
2
2
  module Rails
3
3
  # @private
4
- # Disable some cops until https://github.com/bbatsov/rubocop/issues/1310
5
- # rubocop:disable Style/IndentationConsistency
6
4
  module FeatureCheck
7
- # rubocop:disable Style/IndentationWidth
8
- module_function
9
- # rubocop:enable Style/IndentationWidth
10
-
11
- def can_check_pending_migrations?
12
- has_active_record_migration? &&
13
- ::ActiveRecord::Migration.respond_to?(:check_pending!)
14
- end
15
-
16
- def can_maintain_test_schema?
17
- has_active_record_migration? &&
18
- ::ActiveRecord::Migration.respond_to?(:maintain_test_schema!)
19
- end
20
-
5
+ module_function
21
6
  def has_active_job?
22
7
  defined?(::ActiveJob)
23
8
  end
@@ -38,27 +23,29 @@ module RSpec
38
23
  has_action_mailer? && defined?(::ActionMailer::Preview)
39
24
  end
40
25
 
41
- def has_action_mailer_show_preview?
42
- has_action_mailer_preview? &&
43
- ::ActionMailer::Base.respond_to?(:show_previews=)
26
+ def has_action_cable_testing?
27
+ defined?(::ActionCable)
28
+ end
29
+
30
+ def has_action_mailer_parameterized?
31
+ has_action_mailer? && defined?(::ActionMailer::Parameterized::DeliveryJob)
32
+ end
33
+
34
+ def has_action_mailer_unified_delivery?
35
+ has_action_mailer? && defined?(::ActionMailer::MailDeliveryJob)
44
36
  end
45
37
 
46
- def has_1_9_hash_syntax?
47
- ::Rails::VERSION::STRING > '4.0'
38
+ def has_action_mailer_legacy_delivery_job?
39
+ defined?(ActionMailer::DeliveryJob)
48
40
  end
49
41
 
50
- def has_file_fixture?
51
- ::Rails::VERSION::STRING > '5.0'
42
+ def has_action_mailbox?
43
+ defined?(::ActionMailbox)
52
44
  end
53
45
 
54
46
  def type_metatag(type)
55
- if has_1_9_hash_syntax?
56
- "type: :#{type}"
57
- else
58
- ":type => :#{type}"
59
- end
47
+ "type: :#{type}"
60
48
  end
61
49
  end
62
- # rubocop:enable Style/IndentationConsistency
63
50
  end
64
51
  end
@@ -1,15 +1,16 @@
1
- if ::Rails::VERSION::STRING > '5'
2
- require 'active_support/testing/file_fixtures'
1
+ require 'active_support/testing/file_fixtures'
3
2
 
4
- module RSpec
5
- module Rails
6
- # @private
7
- module FileFixtureSupport
8
- extend ActiveSupport::Concern
9
- include ActiveSupport::Testing::FileFixtures
3
+ module RSpec
4
+ module Rails
5
+ # @private
6
+ module FileFixtureSupport
7
+ extend ActiveSupport::Concern
8
+ include ActiveSupport::Testing::FileFixtures
10
9
 
11
- included do
12
- self.file_fixture_path = RSpec.configuration.file_fixture_path
10
+ included do
11
+ self.file_fixture_path = RSpec.configuration.file_fixture_path
12
+ if defined?(ActiveStorage::FixtureSet)
13
+ ActiveStorage::FixtureSet.file_fixture_path = RSpec.configuration.file_fixture_path
13
14
  end
14
15
  end
15
16
  end
@@ -2,37 +2,36 @@ module RSpec
2
2
  module Rails
3
3
  # @private
4
4
  module FixtureFileUploadSupport
5
- delegate :fixture_file_upload, :to => :rails_fixture_file_wrapper
5
+ delegate :fixture_file_upload, to: :rails_fixture_file_wrapper
6
6
 
7
7
  private
8
8
 
9
+ # In Rails 7.0 fixture file path needs to be relative to `file_fixture_path` instead, this change
10
+ # was brought in with a deprecation warning on 6.1. In Rails 7.0 expect to rework this to remove
11
+ # the old accessor.
9
12
  def rails_fixture_file_wrapper
10
- RailsFixtureFileWrapper.fixture_path = nil
11
- resolved_fixture_path = (fixture_path || RSpec.configuration.fixture_path || '').to_s
12
- RailsFixtureFileWrapper.fixture_path = File.join(resolved_fixture_path, '') unless resolved_fixture_path.strip.empty?
13
+ RailsFixtureFileWrapper.file_fixture_path = nil
14
+ resolved_fixture_path =
15
+ if respond_to?(:file_fixture_path) && !file_fixture_path.nil?
16
+ file_fixture_path.to_s
17
+ else
18
+ (RSpec.configuration.fixture_path || '').to_s
19
+ end
20
+ RailsFixtureFileWrapper.file_fixture_path = File.join(resolved_fixture_path, '') unless resolved_fixture_path.strip.empty?
13
21
  RailsFixtureFileWrapper.instance
14
22
  end
15
23
 
16
24
  class RailsFixtureFileWrapper
17
25
  include ActionDispatch::TestProcess if defined?(ActionDispatch::TestProcess)
26
+ include ActiveSupport::Testing::FileFixtures
18
27
 
19
28
  class << self
20
- attr_reader :fixture_path
29
+ attr_accessor :fixture_path
21
30
 
22
31
  # Get instance of wrapper
23
32
  def instance
24
33
  @instance ||= new
25
34
  end
26
-
27
- # Override fixture_path set
28
- # to support Rails 3.0->3.1 using ActionController::TestCase class to resolve fixture_path
29
- # see https://apidock.com/rails/v3.0.0/ActionDispatch/TestProcess/fixture_file_upload
30
- def fixture_path=(value)
31
- if ActionController::TestCase.respond_to?(:fixture_path)
32
- ActionController::TestCase.fixture_path = value
33
- end
34
- @fixture_path = value
35
- end
36
35
  end
37
36
  end
38
37
  end
@@ -5,52 +5,60 @@ module RSpec
5
5
  if defined?(ActiveRecord::TestFixtures)
6
6
  extend ActiveSupport::Concern
7
7
  include RSpec::Rails::SetupAndTeardownAdapter
8
- include RSpec::Rails::MinitestLifecycleAdapter if ::ActiveRecord::VERSION::STRING > '4'
8
+ include RSpec::Rails::MinitestLifecycleAdapter
9
9
  include RSpec::Rails::MinitestAssertionAdapter
10
10
  include ActiveRecord::TestFixtures
11
11
 
12
+ # @private prevent ActiveSupport::TestFixtures to start a DB transaction.
13
+ # Monkey patched to avoid collisions with 'let(:name)' in Rails 6.1 and after
14
+ # and let(:method_name) before Rails 6.1.
15
+ def run_in_transaction?
16
+ current_example_name = (RSpec.current_example && RSpec.current_example.metadata[:description])
17
+ use_transactional_tests && !self.class.uses_transaction?(current_example_name)
18
+ end
19
+
12
20
  included do
13
- # TODO: (DC 2011-06-25) this is necessary because fixture_file_upload
14
- # accesses fixture_path directly on ActiveSupport::TestCase. This is
15
- # fixed in rails by https://github.com/rails/rails/pull/1861, which
16
- # should be part of the 3.1 release, at which point we can include
17
- # these lines for rails < 3.1.
18
- ActiveSupport::TestCase.class_exec do
19
- include ActiveRecord::TestFixtures
20
- self.fixture_path = RSpec.configuration.fixture_path
21
- end
22
- # /TODO
21
+ if RSpec.configuration.use_active_record?
22
+ include Fixtures
23
23
 
24
- self.fixture_path = RSpec.configuration.fixture_path
25
- if ::Rails::VERSION::STRING > '5'
24
+ # TestFixtures#fixture_path is deprecated and will be removed in Rails 7.2
25
+ if respond_to?(:fixture_paths=)
26
+ fixture_paths << RSpec.configuration.fixture_path
27
+ else
28
+ self.fixture_path = RSpec.configuration.fixture_path
29
+ end
26
30
  self.use_transactional_tests = RSpec.configuration.use_transactional_fixtures
27
- else
28
- self.use_transactional_fixtures = RSpec.configuration.use_transactional_fixtures
31
+ self.use_instantiated_fixtures = RSpec.configuration.use_instantiated_fixtures
32
+
33
+ fixtures RSpec.configuration.global_fixtures if RSpec.configuration.global_fixtures
29
34
  end
30
- self.use_instantiated_fixtures = RSpec.configuration.use_instantiated_fixtures
35
+ end
36
+
37
+ module Fixtures
38
+ extend ActiveSupport::Concern
31
39
 
32
- def self.fixtures(*args)
33
- orig_methods = private_instance_methods
34
- super.tap do
35
- new_methods = private_instance_methods - orig_methods
36
- new_methods.each do |method_name|
37
- proxy_method_warning_if_called_in_before_context_scope(method_name)
40
+ class_methods do
41
+ def fixtures(*args)
42
+ orig_methods = private_instance_methods
43
+ super.tap do
44
+ new_methods = private_instance_methods - orig_methods
45
+ new_methods.each do |method_name|
46
+ proxy_method_warning_if_called_in_before_context_scope(method_name)
47
+ end
38
48
  end
39
49
  end
40
- end
41
50
 
42
- def self.proxy_method_warning_if_called_in_before_context_scope(method_name)
43
- orig_implementation = instance_method(method_name)
44
- define_method(method_name) do |*args, &blk|
45
- if inspect.include?("before(:context)")
46
- RSpec.warn_with("Calling fixture method in before :context ")
47
- else
48
- orig_implementation.bind(self).call(*args, &blk)
51
+ def proxy_method_warning_if_called_in_before_context_scope(method_name)
52
+ orig_implementation = instance_method(method_name)
53
+ define_method(method_name) do |*args, &blk|
54
+ if RSpec.current_scope == :before_context_hook
55
+ RSpec.warn_with("Calling fixture method in before :context ")
56
+ else
57
+ orig_implementation.bind(self).call(*args, &blk)
58
+ end
49
59
  end
50
60
  end
51
61
  end
52
-
53
- fixtures RSpec.configuration.global_fixtures if RSpec.configuration.global_fixtures
54
62
  end
55
63
  end
56
64
  end