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
@@ -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,53 @@ 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)}"
45
+ end
46
+
47
+ if ::Rails::VERSION::STRING.to_f >= 7.1
48
+ # @private
49
+ # Allows failure screenshot to work whilst not exposing metadata
50
+ class SuppressRailsScreenshotMetadata
51
+ def initialize
52
+ @example_data = {}
53
+ end
54
+
55
+ def [](key)
56
+ if @example_data.key?(key)
57
+ @example_data[key]
58
+ else
59
+ raise_wrong_scope_error
60
+ end
61
+ end
62
+
63
+ def []=(key, value)
64
+ if key == :failure_screenshot_path
65
+ @example_data[key] = value
66
+ else
67
+ raise_wrong_scope_error
68
+ end
69
+ end
70
+
71
+ def method_missing(_name, *_args, &_block)
72
+ raise_wrong_scope_error
73
+ end
74
+
75
+ private
76
+
77
+ def raise_wrong_scope_error
78
+ raise RSpec::Core::ExampleGroup::WrongScopeError,
79
+ "`metadata` is not available from within an example " \
80
+ "(e.g. an `it` block) or from constructs that run in the " \
81
+ "scope of an example (e.g. `before`, `let`, etc). It is " \
82
+ "only available on an example group (e.g. a `describe` or "\
83
+ "`context` block)"
84
+ end
85
+ end
86
+
87
+ # @private
88
+ def metadata
89
+ @metadata ||= SuppressRailsScreenshotMetadata.new
90
+ end
41
91
  end
42
92
 
43
93
  # Delegates to `Rails.application`.
@@ -46,26 +96,33 @@ module RSpec
46
96
  end
47
97
 
48
98
  included do |other|
99
+ ActiveSupport.on_load(:action_dispatch_system_test_case) do
100
+ ActionDispatch::SystemTesting::Server.silence_puma = true
101
+ end
102
+
103
+ require 'action_dispatch/system_test_case'
104
+
49
105
  begin
50
106
  require 'capybara'
51
- require 'action_dispatch/system_test_case'
52
107
  rescue LoadError => e
53
108
  abort """
54
109
  LoadError: #{e.message}
55
- System test integration requires Rails >= 5.1 and has a hard
110
+ System test integration has a hard
56
111
  dependency on a webserver and `capybara`, please add capybara to
57
112
  your Gemfile and configure a webserver (e.g. `Capybara.server =
58
- :webrick`) before attempting to use system specs.
113
+ :puma`) before attempting to use system specs.
59
114
  """.gsub(/\s+/, ' ').strip
60
115
  end
61
116
 
117
+ original_before_teardown =
118
+ ::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown.instance_method(:before_teardown)
119
+
62
120
  original_after_teardown =
63
121
  ::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown.instance_method(:after_teardown)
64
122
 
65
- other.include ActionDispatch::IntegrationTest::Behavior
66
123
  other.include ::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown
67
124
  other.include ::ActionDispatch::SystemTesting::TestHelpers::ScreenshotHelper
68
- other.include BlowAwayAfterTeardownHook
125
+ other.include BlowAwayTeardownHooks
69
126
 
70
127
  attr_reader :driver
71
128
 
@@ -76,16 +133,19 @@ module RSpec
76
133
  def initialize(*args, &blk)
77
134
  super(*args, &blk)
78
135
  @driver = nil
136
+
137
+ self.class.before do
138
+ # A user may have already set the driver, so only default if driver
139
+ # is not set
140
+ driven_by(:selenium) unless @driver
141
+ end
79
142
  end
80
143
 
81
- def driven_by(*args, &blk)
82
- @driver = ::ActionDispatch::SystemTestCase.driven_by(*args, &blk).tap(&:use)
144
+ def driven_by(driver, **driver_options, &blk)
145
+ @driver = ::ActionDispatch::SystemTestCase.driven_by(driver, **driver_options, &blk).tap(&:use)
83
146
  end
84
147
 
85
148
  before do
86
- # A user may have already set the driver, so only default if driver
87
- # is not set
88
- driven_by(:selenium) unless @driver
89
149
  @routes = ::Rails.application.routes
90
150
  end
91
151
 
@@ -93,13 +153,19 @@ module RSpec
93
153
  orig_stdout = $stdout
94
154
  $stdout = StringIO.new
95
155
  begin
96
- original_after_teardown.bind(self).call
156
+ original_before_teardown.bind(self).call
97
157
  ensure
98
158
  myio = $stdout
99
- RSpec.current_example.metadata[:extra_failure_lines] = myio.string
159
+ myio.rewind
160
+ RSpec.current_example.metadata[:extra_failure_lines] = myio.readlines
100
161
  $stdout = orig_stdout
101
162
  end
102
163
  end
164
+
165
+ around do |example|
166
+ example.run
167
+ original_after_teardown.bind(self).call
168
+ end
103
169
  end
104
170
  end
105
171
  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,12 +59,13 @@ 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
69
66
  def render(options = {}, local_assigns = {}, &block)
70
67
  options = _default_render_options if Hash === options && options.empty?
68
+ options = options.merge(_default_render_options) if Hash === options && options.keys == [:locals]
71
69
  super(options, local_assigns, &block)
72
70
  end
73
71
 
@@ -92,7 +90,7 @@ module RSpec
92
90
  #
93
91
  # stub_template("widgets/_widget.html.erb" => "This content.")
94
92
  def stub_template(hash)
95
- view.view_paths.unshift(StubResolverCache.resolver_for(hash))
93
+ controller.prepend_view_path(StubResolverCache.resolver_for(hash))
96
94
  end
97
95
 
98
96
  # Provides access to the params hash that will be available within the
@@ -105,7 +103,7 @@ module RSpec
105
103
 
106
104
  # @deprecated Use `view` instead.
107
105
  def template
108
- RSpec.deprecate("template", :replacement => "view")
106
+ RSpec.deprecate("template", replacement: "view")
109
107
  view
110
108
  end
111
109
 
@@ -128,20 +126,37 @@ module RSpec
128
126
  private
129
127
 
130
128
  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
129
+ formats = if ActionView::Template::Types.respond_to?(:symbols)
130
+ ActionView::Template::Types.symbols
131
+ else
132
+ [:html, :text, :js, :css, :xml, :json].map(&:to_s)
133
+ end.map { |x| Regexp.escape(x) }.join("|")
134
+
135
+ handlers = ActionView::Template::Handlers.extensions.map { |x| Regexp.escape(x) }.join("|")
136
+ locales = "[a-z]{2}(?:-[A-Z]{2})?"
137
+ variants = "[^.]*"
138
+ path_regex = %r{
139
+ \A
140
+ (?<template>.*?)
141
+ (?:\.(?<locale>#{locales}))??
142
+ (?:\.(?<format>#{formats}))??
143
+ (?:\+(?<variant>#{variants}))??
144
+ (?:\.(?<handler>#{handlers}))?
145
+ \z
146
+ }x
147
+
148
+ # This regex should always find a match.
149
+ # Worst case, everything will be nil, and :template will just be
150
+ # the original string.
151
+ match = path_regex.match(_default_file_to_render)
152
+
153
+ render_options = { template: match[:template] }
154
+ render_options[:handlers] = [match[:handler].to_sym] if match[:handler]
155
+ render_options[:formats] = [match[:format].to_sym] if match[:format]
156
+ render_options[:locales] = [match[:locale].to_sym] if match[:locale]
157
+ render_options[:variants] = [match[:variant].to_sym] if match[:variant]
158
+
159
+ render_options
145
160
  end
146
161
 
147
162
  def _path_parts
@@ -171,10 +186,7 @@ module RSpec
171
186
 
172
187
  before do
173
188
  _include_controller_helpers
174
- if view.lookup_context.respond_to?(:prefixes)
175
- # rails 3.1
176
- view.lookup_context.prefixes << _controller_path
177
- end
189
+ view.lookup_context.prefixes << _controller_path
178
190
 
179
191
  controller.controller_path = _controller_path
180
192
 
@@ -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
@@ -3,16 +3,6 @@ module RSpec
3
3
  # @private
4
4
  module FeatureCheck
5
5
  module_function
6
- def can_check_pending_migrations?
7
- has_active_record_migration? &&
8
- ::ActiveRecord::Migration.respond_to?(:check_pending!)
9
- end
10
-
11
- def can_maintain_test_schema?
12
- has_active_record_migration? &&
13
- ::ActiveRecord::Migration.respond_to?(:maintain_test_schema!)
14
- end
15
-
16
6
  def has_active_job?
17
7
  defined?(::ActiveJob)
18
8
  end
@@ -33,25 +23,28 @@ module RSpec
33
23
  has_action_mailer? && defined?(::ActionMailer::Preview)
34
24
  end
35
25
 
36
- def has_action_mailer_show_preview?
37
- has_action_mailer_preview? &&
38
- ::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)
39
36
  end
40
37
 
41
- def has_1_9_hash_syntax?
42
- ::Rails::VERSION::STRING > '4.0'
38
+ def has_action_mailer_legacy_delivery_job?
39
+ defined?(ActionMailer::DeliveryJob)
43
40
  end
44
41
 
45
- def has_file_fixture?
46
- ::Rails::VERSION::STRING > '5.0'
42
+ def has_action_mailbox?
43
+ defined?(::ActionMailbox)
47
44
  end
48
45
 
49
46
  def type_metatag(type)
50
- if has_1_9_hash_syntax?
51
- "type: :#{type}"
52
- else
53
- ":type => :#{type}"
54
- end
47
+ "type: :#{type}"
55
48
  end
56
49
  end
57
50
  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,42 @@ 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
+ elsif respond_to?(:fixture_paths)
18
+ (RSpec.configuration.fixture_paths&.first || '').to_s
19
+ else
20
+ (RSpec.configuration.fixture_path || '').to_s
21
+ end
22
+ RailsFixtureFileWrapper.file_fixture_path = File.join(resolved_fixture_path, '') unless resolved_fixture_path.strip.empty?
13
23
  RailsFixtureFileWrapper.instance
14
24
  end
15
25
 
16
26
  class RailsFixtureFileWrapper
17
27
  include ActionDispatch::TestProcess if defined?(ActionDispatch::TestProcess)
28
+ include ActiveSupport::Testing::FileFixtures
18
29
 
19
30
  class << self
20
- attr_reader :fixture_path
31
+ if ::Rails::VERSION::STRING < "7.1.0"
32
+ attr_accessor :fixture_path
33
+ else
34
+ attr_accessor :fixture_paths
35
+ end
21
36
 
22
37
  # Get instance of wrapper
23
38
  def instance
24
39
  @instance ||= new
25
40
  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
41
  end
37
42
  end
38
43
  end
@@ -5,52 +5,83 @@ 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
+
24
+ # TestFixtures#fixture_path is deprecated and will be removed in Rails 7.2
25
+ if respond_to?(:fixture_paths=)
26
+ self.fixture_paths = RSpec.configuration.fixture_paths
27
+ else
28
+ self.fixture_path = RSpec.configuration.fixture_path
29
+ end
23
30
 
24
- self.fixture_path = RSpec.configuration.fixture_path
25
- if ::Rails::VERSION::STRING > '5'
26
31
  self.use_transactional_tests = RSpec.configuration.use_transactional_fixtures
27
- else
28
- self.use_transactional_fixtures = RSpec.configuration.use_transactional_fixtures
32
+ self.use_instantiated_fixtures = RSpec.configuration.use_instantiated_fixtures
33
+
34
+ fixtures RSpec.configuration.global_fixtures if RSpec.configuration.global_fixtures
29
35
  end
30
- self.use_instantiated_fixtures = RSpec.configuration.use_instantiated_fixtures
31
-
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)
36
+ end
37
+
38
+ module Fixtures
39
+ extend ActiveSupport::Concern
40
+
41
+ # rubocop:disable Metrics/BlockLength
42
+ class_methods do
43
+ if ::Rails.version.to_f >= 7.1
44
+ def fixtures(*args)
45
+ super.tap do
46
+ fixture_sets.each_pair do |method_name, fixture_name|
47
+ proxy_method_warning_if_called_in_before_context_scope(method_name, fixture_name)
48
+ end
49
+ end
38
50
  end
39
- end
40
- end
41
51
 
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)
52
+ def proxy_method_warning_if_called_in_before_context_scope(method_name, fixture_name)
53
+ define_method(method_name) do |*args, **kwargs, &blk|
54
+ if RSpec.current_scope == :before_context_hook
55
+ RSpec.warn_with("Calling fixture method in before :context ")
56
+ else
57
+ access_fixture(fixture_name, *args, **kwargs, &blk)
58
+ end
59
+ end
60
+ end
61
+ else
62
+ def fixtures(*args)
63
+ orig_methods = private_instance_methods
64
+ super.tap do
65
+ new_methods = private_instance_methods - orig_methods
66
+ new_methods.each do |method_name|
67
+ proxy_method_warning_if_called_in_before_context_scope(method_name)
68
+ end
69
+ end
70
+ end
71
+
72
+ def proxy_method_warning_if_called_in_before_context_scope(method_name)
73
+ orig_implementation = instance_method(method_name)
74
+ define_method(method_name) do |*args, &blk|
75
+ if RSpec.current_scope == :before_context_hook
76
+ RSpec.warn_with("Calling fixture method in before :context ")
77
+ else
78
+ orig_implementation.bind(self).call(*args, &blk)
79
+ end
80
+ end
49
81
  end
50
82
  end
51
83
  end
52
-
53
- fixtures RSpec.configuration.global_fixtures if RSpec.configuration.global_fixtures
84
+ # rubocop:enable Metrics/BlockLength
54
85
  end
55
86
  end
56
87
  end