rspec-rails 3.7.0 → 4.0.0

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 (81) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data/Capybara.md +5 -54
  4. data/Changelog.md +259 -70
  5. data/README.md +265 -496
  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 +4 -4
  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 +25 -12
  17. data/lib/generators/rspec/integration/integration_generator.rb +4 -4
  18. data/lib/generators/rspec/integration/templates/request_spec.rb +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 +2 -1
  22. data/lib/generators/rspec/mailer/templates/preview.rb +1 -1
  23. data/lib/generators/rspec/model/model_generator.rb +6 -5
  24. data/lib/generators/rspec/model/templates/fixtures.yml +1 -1
  25. data/lib/generators/rspec/request/request_generator.rb +1 -1
  26. data/lib/generators/rspec/scaffold/scaffold_generator.rb +43 -23
  27. data/lib/generators/rspec/scaffold/templates/api_controller_spec.rb +2 -38
  28. data/lib/generators/rspec/scaffold/templates/api_request_spec.rb +131 -0
  29. data/lib/generators/rspec/scaffold/templates/controller_spec.rb +17 -17
  30. data/lib/generators/rspec/scaffold/templates/edit_spec.rb +1 -1
  31. data/lib/generators/rspec/scaffold/templates/index_spec.rb +2 -2
  32. data/lib/generators/rspec/scaffold/templates/new_spec.rb +1 -1
  33. data/lib/generators/rspec/scaffold/templates/request_spec.rb +133 -0
  34. data/lib/generators/rspec/scaffold/templates/routing_spec.rb +10 -13
  35. data/lib/generators/rspec/scaffold/templates/show_spec.rb +1 -1
  36. data/lib/generators/rspec/system/system_generator.rb +26 -0
  37. data/lib/generators/rspec/system/templates/system_spec.rb +9 -0
  38. data/lib/generators/rspec/view/view_generator.rb +2 -2
  39. data/lib/generators/rspec.rb +0 -6
  40. data/lib/rspec/rails/adapters.rb +11 -76
  41. data/lib/rspec/rails/configuration.rb +47 -36
  42. data/lib/rspec/rails/example/channel_example_group.rb +93 -0
  43. data/lib/rspec/rails/example/controller_example_group.rb +4 -4
  44. data/lib/rspec/rails/example/feature_example_group.rb +6 -26
  45. data/lib/rspec/rails/example/helper_example_group.rb +2 -9
  46. data/lib/rspec/rails/example/mailbox_example_group.rb +80 -0
  47. data/lib/rspec/rails/example/mailer_example_group.rb +1 -1
  48. data/lib/rspec/rails/example/rails_example_group.rb +1 -1
  49. data/lib/rspec/rails/example/system_example_group.rb +96 -60
  50. data/lib/rspec/rails/example/view_example_group.rb +47 -28
  51. data/lib/rspec/rails/example.rb +3 -3
  52. data/lib/rspec/rails/extensions/active_record/proxy.rb +1 -9
  53. data/lib/rspec/rails/feature_check.rb +12 -29
  54. data/lib/rspec/rails/fixture_file_upload_support.rb +40 -0
  55. data/lib/rspec/rails/fixture_support.rb +37 -31
  56. data/lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb +170 -0
  57. data/lib/rspec/rails/matchers/action_cable/have_streams.rb +58 -0
  58. data/lib/rspec/rails/matchers/action_cable.rb +65 -0
  59. data/lib/rspec/rails/matchers/action_mailbox.rb +64 -0
  60. data/lib/rspec/rails/matchers/active_job.rb +180 -22
  61. data/lib/rspec/rails/matchers/base_matcher.rb +179 -0
  62. data/lib/rspec/rails/matchers/be_a_new.rb +1 -1
  63. data/lib/rspec/rails/matchers/be_new_record.rb +1 -1
  64. data/lib/rspec/rails/matchers/be_valid.rb +1 -1
  65. data/lib/rspec/rails/matchers/have_enqueued_mail.rb +198 -0
  66. data/lib/rspec/rails/matchers/have_http_status.rb +34 -13
  67. data/lib/rspec/rails/matchers/have_rendered.rb +2 -1
  68. data/lib/rspec/rails/matchers/redirect_to.rb +1 -1
  69. data/lib/rspec/rails/matchers/routing_matchers.rb +14 -14
  70. data/lib/rspec/rails/matchers.rb +11 -0
  71. data/lib/rspec/rails/tasks/rspec.rake +7 -17
  72. data/lib/rspec/rails/vendor/capybara.rb +10 -15
  73. data/lib/rspec/rails/version.rb +1 -1
  74. data/lib/rspec/rails/view_path_builder.rb +1 -1
  75. data/lib/rspec/rails/view_rendering.rb +16 -5
  76. data/lib/rspec/rails.rb +1 -0
  77. data/lib/rspec-rails.rb +13 -10
  78. data.tar.gz.sig +0 -0
  79. metadata +55 -33
  80. metadata.gz.sig +0 -0
  81. data/lib/generators/rspec/observer/observer_generator.rb +0 -13
@@ -1,85 +1,121 @@
1
- if ActionPack::VERSION::STRING >= "5.1"
2
- require 'action_dispatch/system_test_case'
3
- module RSpec
4
- module Rails
5
- # @api public
6
- # Container class for system tests
7
- module SystemExampleGroup
8
- extend ActiveSupport::Concern
9
- include RSpec::Rails::RailsExampleGroup
10
- include ActionDispatch::Integration::Runner
11
- include ActionDispatch::Assertions
12
- include RSpec::Rails::Matchers::RedirectTo
13
- include RSpec::Rails::Matchers::RenderTemplate
14
- include ActionController::TemplateAssertions
15
-
16
- include ActionDispatch::IntegrationTest::Behavior
1
+ module RSpec
2
+ module Rails
3
+ # @api public
4
+ # Container class for system tests
5
+ module SystemExampleGroup
6
+ extend ActiveSupport::Concern
7
+ include RSpec::Rails::RailsExampleGroup
8
+ include RSpec::Rails::Matchers::RedirectTo
9
+ include RSpec::Rails::Matchers::RenderTemplate
10
+ include ActionDispatch::Integration::Runner
11
+ include ActionDispatch::Assertions
12
+ include ActionController::TemplateAssertions
17
13
 
14
+ # Special characters to translate into underscores for #method_name
15
+ CHARS_TO_TRANSLATE = ['/', '.', ':', ',', "'", '"', " "].freeze
16
+
17
+ # @private
18
+ module BlowAwayTeardownHooks
18
19
  # @private
19
- module BlowAwayAfterTeardownHook
20
- # @private
21
- def after_teardown
22
- end
20
+ def before_teardown
21
+ end
22
+
23
+ # @private
24
+ def after_teardown
23
25
  end
26
+ end
24
27
 
25
- original_after_teardown = ::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown.instance_method(:after_teardown)
28
+ # for the SystemTesting Screenshot situation
29
+ def passed?
30
+ return false if RSpec.current_example.exception
31
+ return true unless defined?(::RSpec::Expectations::FailureAggregator)
26
32
 
27
- include ::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown
28
- include ::ActionDispatch::SystemTesting::TestHelpers::ScreenshotHelper
29
- include BlowAwayAfterTeardownHook
33
+ failure_notifier = ::RSpec::Support.failure_notifier
34
+ return true unless failure_notifier.is_a?(::RSpec::Expectations::FailureAggregator)
35
+
36
+ failure_notifier.failures.empty? && failure_notifier.other_errors.empty?
37
+ end
38
+
39
+ # @private
40
+ def method_name
41
+ @method_name ||= [
42
+ self.class.name.underscore,
43
+ RSpec.current_example.description.underscore
44
+ ].join("_").tr(CHARS_TO_TRANSLATE.join, "_")[0...200] + "_#{rand(1000)}"
45
+ end
46
+
47
+ # Delegates to `Rails.application`.
48
+ def app
49
+ ::Rails.application
50
+ end
30
51
 
31
- # for the SystemTesting Screenshot situation
32
- def passed?
33
- RSpec.current_example.exception.nil?
52
+ included do |other|
53
+ ActiveSupport.on_load(:action_dispatch_system_test_case) do
54
+ ActionDispatch::SystemTesting::Server.silence_puma = true
34
55
  end
35
56
 
36
- # @private
37
- def method_name
38
- @method_name ||= [
39
- self.class.name.underscore,
40
- RSpec.current_example.description.underscore,
41
- rand(1000)
42
- ].join("_").gsub(/[\/\.:, ]/, "_")
57
+ begin
58
+ require 'capybara'
59
+ require 'action_dispatch/system_test_case'
60
+ rescue LoadError => e
61
+ abort """
62
+ LoadError: #{e.message}
63
+ System test integration requires Rails >= 5.1 and has a hard
64
+ dependency on a webserver and `capybara`, please add capybara to
65
+ your Gemfile and configure a webserver (e.g. `Capybara.server =
66
+ :webrick`) before attempting to use system specs.
67
+ """.gsub(/\s+/, ' ').strip
43
68
  end
44
69
 
45
- # Delegates to `Rails.application`.
46
- def app
47
- ::Rails.application
70
+ if ::Rails::VERSION::STRING >= '6.0'
71
+ original_before_teardown =
72
+ ::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown.instance_method(:before_teardown)
48
73
  end
49
74
 
50
- included do
51
- attr_reader :driver
75
+ original_after_teardown =
76
+ ::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown.instance_method(:after_teardown)
52
77
 
53
- if ActionDispatch::SystemTesting::Server.respond_to?(:silence_puma=)
54
- ActionDispatch::SystemTesting::Server.silence_puma = true
55
- end
78
+ other.include ::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown
79
+ other.include ::ActionDispatch::SystemTesting::TestHelpers::ScreenshotHelper
80
+ other.include BlowAwayTeardownHooks
56
81
 
57
- def initialize(*args, &blk)
58
- super(*args, &blk)
59
- @driver = nil
60
- end
82
+ attr_reader :driver
61
83
 
62
- def driven_by(*args, &blk)
63
- @driver = ::ActionDispatch::SystemTestCase.driven_by(*args, &blk).tap(&:use)
64
- end
84
+ if ActionDispatch::SystemTesting::Server.respond_to?(:silence_puma=)
85
+ ActionDispatch::SystemTesting::Server.silence_puma = true
86
+ end
87
+
88
+ def initialize(*args, &blk)
89
+ super(*args, &blk)
90
+ @driver = nil
65
91
 
66
- before do
92
+ self.class.before do
67
93
  # A user may have already set the driver, so only default if driver
68
94
  # is not set
69
95
  driven_by(:selenium) unless @driver
70
- @routes = ::Rails.application.routes
71
96
  end
97
+ end
98
+
99
+ def driven_by(*args, &blk)
100
+ @driver = ::ActionDispatch::SystemTestCase.driven_by(*args, &blk).tap(&:use)
101
+ end
102
+
103
+ before do
104
+ @routes = ::Rails.application.routes
105
+ end
72
106
 
73
- after do
74
- orig_stdout = $stdout
75
- $stdout = StringIO.new
76
- begin
77
- original_after_teardown.bind(self).call
78
- ensure
79
- myio = $stdout
80
- RSpec.current_example.metadata[:extra_failure_lines] = myio.string
81
- $stdout = orig_stdout
107
+ after do
108
+ orig_stdout = $stdout
109
+ $stdout = StringIO.new
110
+ begin
111
+ if ::Rails::VERSION::STRING >= '6.0'
112
+ original_before_teardown.bind(self).call
82
113
  end
114
+ original_after_teardown.bind(self).call
115
+ ensure
116
+ myio = $stdout
117
+ RSpec.current_example.metadata[:extra_failure_lines] = myio.string
118
+ $stdout = orig_stdout
83
119
  end
84
120
  end
85
121
  end
@@ -13,6 +13,14 @@ module RSpec
13
13
  include RSpec::Rails::ViewAssigns
14
14
  include RSpec::Rails::Matchers::RenderTemplate
15
15
 
16
+ # @private
17
+ module StubResolverCache
18
+ def self.resolver_for(hash)
19
+ @resolvers ||= {}
20
+ @resolvers[hash] ||= ActionView::FixtureResolver.new(hash)
21
+ end
22
+ end
23
+
16
24
  # @private
17
25
  module ClassMethods
18
26
  def _default_helper
@@ -35,16 +43,13 @@ module RSpec
35
43
 
36
44
  included do
37
45
  include ::Rails.application.routes.url_helpers
38
-
39
- if ::Rails.application.routes.respond_to?(:mounted_helpers)
40
- include ::Rails.application.routes.mounted_helpers
41
- end
46
+ include ::Rails.application.routes.mounted_helpers
42
47
  end
43
48
 
44
49
  # @overload render
45
- # @overload render({:partial => path_to_file})
46
- # @overload render({:partial => path_to_file}, {... locals ...})
47
- # @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
48
53
  #
49
54
  # Delegates to ActionView::Base#render, so see documentation on that
50
55
  # for more info.
@@ -54,7 +59,7 @@ module RSpec
54
59
  #
55
60
  # describe "widgets/new.html.erb" do
56
61
  # it "shows all the widgets" do
57
- # render # => view.render(:file => "widgets/new.html.erb")
62
+ # render # => view.render(file: "widgets/new.html.erb")
58
63
  # # ...
59
64
  # end
60
65
  # end
@@ -84,7 +89,7 @@ module RSpec
84
89
  #
85
90
  # stub_template("widgets/_widget.html.erb" => "This content.")
86
91
  def stub_template(hash)
87
- view.view_paths.unshift(ActionView::FixtureResolver.new(hash))
92
+ view.view_paths.unshift(StubResolverCache.resolver_for(hash))
88
93
  end
89
94
 
90
95
  # Provides access to the params hash that will be available within the
@@ -97,7 +102,7 @@ module RSpec
97
102
 
98
103
  # @deprecated Use `view` instead.
99
104
  def template
100
- RSpec.deprecate("template", :replacement => "view")
105
+ RSpec.deprecate("template", replacement: "view")
101
106
  view
102
107
  end
103
108
 
@@ -120,20 +125,37 @@ module RSpec
120
125
  private
121
126
 
122
127
  def _default_render_options
123
- if ::Rails::VERSION::STRING >= '3.2'
124
- # pluck the handler, format, and locale out of, eg, posts/index.de.html.haml
125
- template, *components = _default_file_to_render.split('.')
126
- handler, format, locale = *components.reverse
127
-
128
- render_options = { :template => template }
129
- render_options[:handlers] = [handler] if handler
130
- render_options[:formats] = [format.to_sym] if format
131
- render_options[:locales] = [locale] if locale
132
-
133
- render_options
134
- else
135
- { :template => _default_file_to_render }
136
- 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]] if match[:handler]
154
+ render_options[:formats] = [match[:format].to_sym] if match[:format]
155
+ render_options[:locales] = [match[:locale]] if match[:locale]
156
+ render_options[:variants] = [match[:variant]] if match[:variant]
157
+
158
+ render_options
137
159
  end
138
160
 
139
161
  def _path_parts
@@ -163,10 +185,7 @@ module RSpec
163
185
 
164
186
  before do
165
187
  _include_controller_helpers
166
- if view.lookup_context.respond_to?(:prefixes)
167
- # rails 3.1
168
- view.lookup_context.prefixes << _controller_path
169
- end
188
+ view.lookup_context.prefixes << _controller_path
170
189
 
171
190
  controller.controller_path = _controller_path
172
191
 
@@ -8,6 +8,6 @@ require 'rspec/rails/example/routing_example_group'
8
8
  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
- if ActionPack::VERSION::STRING >= "5.1"
12
- require 'rspec/rails/example/system_example_group'
13
- end
11
+ require 'rspec/rails/example/system_example_group'
12
+ require 'rspec/rails/example/channel_example_group'
13
+ require 'rspec/rails/example/mailbox_example_group'
@@ -2,15 +2,7 @@ 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
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
5
+ RSpec::Matchers.configuration.add_should_and_should_not_to ActiveRecord::Associations::CollectionProxy
14
6
  end
15
7
  end
16
8
  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,25 @@ 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) && ActionCable::VERSION::MAJOR >= 6
28
+ end
29
+
30
+ def has_action_mailer_parameterized?
31
+ has_action_mailer? && defined?(::ActionMailer::Parameterized)
44
32
  end
45
33
 
46
- def has_1_9_hash_syntax?
47
- ::Rails::VERSION::STRING > '4.0'
34
+ def has_action_mailer_unified_delivery?
35
+ has_action_mailer? && defined?(::ActionMailer::MailDeliveryJob)
48
36
  end
49
37
 
50
- def has_file_fixture?
51
- ::Rails::VERSION::STRING > '5.0'
38
+ def has_action_mailbox?
39
+ defined?(::ActionMailbox)
52
40
  end
53
41
 
54
42
  def type_metatag(type)
55
- if has_1_9_hash_syntax?
56
- "type: :#{type}"
57
- else
58
- ":type => :#{type}"
59
- end
43
+ "type: :#{type}"
60
44
  end
61
45
  end
62
- # rubocop:enable Style/IndentationConsistency
63
46
  end
64
47
  end
@@ -0,0 +1,40 @@
1
+ module RSpec
2
+ module Rails
3
+ # @private
4
+ module FixtureFileUploadSupport
5
+ delegate :fixture_file_upload, to: :rails_fixture_file_wrapper
6
+
7
+ private
8
+
9
+ 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.instance
14
+ end
15
+
16
+ class RailsFixtureFileWrapper
17
+ include ActionDispatch::TestProcess if defined?(ActionDispatch::TestProcess)
18
+
19
+ class << self
20
+ attr_reader :fixture_path
21
+
22
+ # Get instance of wrapper
23
+ def instance
24
+ @instance ||= new
25
+ 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
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -5,52 +5,58 @@ 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
12
  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
13
+ if RSpec.configuration.use_active_record?
14
+ include Fixtures
15
+
20
16
  self.fixture_path = RSpec.configuration.fixture_path
21
- end
22
- # /TODO
17
+ if ::Rails::VERSION::STRING > '5'
18
+ self.use_transactional_tests = RSpec.configuration.use_transactional_fixtures
19
+ else
20
+ self.use_transactional_fixtures = RSpec.configuration.use_transactional_fixtures
21
+ end
22
+ self.use_instantiated_fixtures = RSpec.configuration.use_instantiated_fixtures
23
23
 
24
- self.fixture_path = RSpec.configuration.fixture_path
25
- if ::Rails::VERSION::STRING > '5'
26
- self.use_transactional_tests = RSpec.configuration.use_transactional_fixtures
27
- else
28
- self.use_transactional_fixtures = RSpec.configuration.use_transactional_fixtures
24
+ fixtures RSpec.configuration.global_fixtures if RSpec.configuration.global_fixtures
29
25
  end
30
- self.use_instantiated_fixtures = RSpec.configuration.use_instantiated_fixtures
26
+ end
31
27
 
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)
28
+ module Fixtures
29
+ extend ActiveSupport::Concern
30
+
31
+ class_methods do
32
+ def 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)
38
+ end
38
39
  end
39
40
  end
40
- end
41
41
 
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)
42
+ def 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)
49
+ end
49
50
  end
50
51
  end
51
52
  end
52
53
 
53
- fixtures RSpec.configuration.global_fixtures if RSpec.configuration.global_fixtures
54
+ if ::Rails.version.to_f >= 6.1
55
+ # @private return the example name for TestFixtures
56
+ def name
57
+ @example
58
+ end
59
+ end
54
60
  end
55
61
  end
56
62
  end