rspec-rails 3.9.1 → 5.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/Capybara.md +5 -54
  5. data/Changelog.md +207 -77
  6. data/README.md +27 -21
  7. data/lib/generators/rspec.rb +0 -6
  8. data/lib/generators/rspec/channel/channel_generator.rb +12 -0
  9. data/lib/generators/rspec/{observer/templates/observer_spec.rb → channel/templates/channel_spec.rb.erb} +1 -1
  10. data/lib/generators/rspec/controller/controller_generator.rb +22 -5
  11. data/lib/generators/rspec/controller/templates/request_spec.rb +19 -0
  12. data/lib/generators/rspec/controller/templates/routing_spec.rb +13 -0
  13. data/lib/generators/rspec/feature/feature_generator.rb +2 -2
  14. data/lib/generators/rspec/{generators → generator}/generator_generator.rb +2 -2
  15. data/lib/generators/rspec/{generators → generator}/templates/generator_spec.rb +0 -0
  16. data/lib/generators/rspec/helper/helper_generator.rb +1 -1
  17. data/lib/generators/rspec/install/install_generator.rb +4 -4
  18. data/lib/generators/rspec/install/templates/spec/rails_helper.rb +17 -16
  19. data/lib/generators/rspec/integration/integration_generator.rb +3 -3
  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 +5 -4
  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 +29 -19
  29. data/lib/generators/rspec/scaffold/templates/api_controller_spec.rb +0 -36
  30. data/lib/generators/rspec/scaffold/templates/api_request_spec.rb +131 -0
  31. data/lib/generators/rspec/scaffold/templates/controller_spec.rb +0 -48
  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 +137 -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 +14 -16
  39. data/lib/generators/rspec/view/view_generator.rb +2 -2
  40. data/lib/rspec-rails.rb +13 -16
  41. data/lib/rspec/rails/adapters.rb +11 -76
  42. data/lib/rspec/rails/configuration.rb +81 -37
  43. data/lib/rspec/rails/example.rb +2 -0
  44. data/lib/rspec/rails/example/channel_example_group.rb +93 -0
  45. data/lib/rspec/rails/example/controller_example_group.rb +4 -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/extensions/active_record/proxy.rb +5 -10
  55. data/lib/rspec/rails/feature_check.rb +12 -29
  56. data/lib/rspec/rails/file_fixture_support.rb +9 -11
  57. data/lib/rspec/rails/fixture_file_upload_support.rb +33 -17
  58. data/lib/rspec/rails/fixture_support.rb +34 -32
  59. data/lib/rspec/rails/matchers.rb +9 -0
  60. data/lib/rspec/rails/matchers/action_cable.rb +65 -0
  61. data/lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb +170 -0
  62. data/lib/rspec/rails/matchers/action_cable/have_streams.rb +58 -0
  63. data/lib/rspec/rails/matchers/action_mailbox.rb +73 -0
  64. data/lib/rspec/rails/matchers/active_job.rb +169 -21
  65. data/lib/rspec/rails/matchers/base_matcher.rb +5 -10
  66. data/lib/rspec/rails/matchers/have_enqueued_mail.rb +52 -28
  67. data/lib/rspec/rails/matchers/have_http_status.rb +11 -7
  68. data/lib/rspec/rails/matchers/have_rendered.rb +1 -0
  69. data/lib/rspec/rails/matchers/relation_match_array.rb +1 -1
  70. data/lib/rspec/rails/matchers/routing_matchers.rb +12 -12
  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 +15 -4
  76. metadata +47 -35
  77. metadata.gz.sig +0 -0
  78. data/lib/generators/rspec/observer/observer_generator.rb +0 -13
@@ -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,7 +9,7 @@ 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
15
  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,12 +50,14 @@ 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
+
49
57
  begin
50
58
  require 'capybara'
51
59
  require 'action_dispatch/system_test_case'
52
- # rubocop:disable Lint/HandleExceptions
53
60
  rescue LoadError => e
54
- # rubocop:enable Lint/HandleExceptions
55
61
  abort """
56
62
  LoadError: #{e.message}
57
63
  System test integration requires Rails >= 5.1 and has a hard
@@ -61,13 +67,17 @@ module RSpec
61
67
  """.gsub(/\s+/, ' ').strip
62
68
  end
63
69
 
70
+ if ::Rails::VERSION::STRING >= '6.0'
71
+ original_before_teardown =
72
+ ::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown.instance_method(:before_teardown)
73
+ end
74
+
64
75
  original_after_teardown =
65
76
  ::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown.instance_method(:after_teardown)
66
77
 
67
- other.include ActionDispatch::IntegrationTest::Behavior
68
78
  other.include ::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown
69
79
  other.include ::ActionDispatch::SystemTesting::TestHelpers::ScreenshotHelper
70
- other.include BlowAwayAfterTeardownHook
80
+ other.include BlowAwayTeardownHooks
71
81
 
72
82
  attr_reader :driver
73
83
 
@@ -78,16 +88,19 @@ module RSpec
78
88
  def initialize(*args, &blk)
79
89
  super(*args, &blk)
80
90
  @driver = nil
91
+
92
+ self.class.before do
93
+ # A user may have already set the driver, so only default if driver
94
+ # is not set
95
+ driven_by(:selenium) unless @driver
96
+ end
81
97
  end
82
98
 
83
- def driven_by(*args, &blk)
84
- @driver = ::ActionDispatch::SystemTestCase.driven_by(*args, &blk).tap(&:use)
99
+ def driven_by(driver, **driver_options, &blk)
100
+ @driver = ::ActionDispatch::SystemTestCase.driven_by(driver, **driver_options, &blk).tap(&:use)
85
101
  end
86
102
 
87
103
  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
104
  @routes = ::Rails.application.routes
92
105
  end
93
106
 
@@ -95,10 +108,14 @@ module RSpec
95
108
  orig_stdout = $stdout
96
109
  $stdout = StringIO.new
97
110
  begin
111
+ if ::Rails::VERSION::STRING >= '6.0'
112
+ original_before_teardown.bind(self).call
113
+ end
98
114
  original_after_teardown.bind(self).call
99
115
  ensure
100
116
  myio = $stdout
101
- RSpec.current_example.metadata[:extra_failure_lines] = myio.string
117
+ myio.rewind
118
+ RSpec.current_example.metadata[:extra_failure_lines] = myio.readlines
102
119
  $stdout = orig_stdout
103
120
  end
104
121
  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]] 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
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
 
@@ -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,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
@@ -1,16 +1,14 @@
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
13
- end
10
+ included do
11
+ self.file_fixture_path = RSpec.configuration.file_fixture_path
14
12
  end
15
13
  end
16
14
  end
@@ -2,37 +2,53 @@ 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
- 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
9
+ # In Rails 6.2 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 6.2 expect to rework this to remove
11
+ # the old accessor.
12
+ if ::Rails.version.to_f >= 6.1
13
+ def rails_fixture_file_wrapper
14
+ RailsFixtureFileWrapper.file_fixture_path = nil
15
+ resolved_fixture_path =
16
+ if respond_to?(:file_fixture_path) && !file_fixture_path.nil?
17
+ file_fixture_path.to_s
18
+ else
19
+ (RSpec.configuration.fixture_path || '').to_s
20
+ end
21
+ RailsFixtureFileWrapper.file_fixture_path = File.join(resolved_fixture_path, '') unless resolved_fixture_path.strip.empty?
22
+ RailsFixtureFileWrapper.instance
23
+ end
24
+ else
25
+ def rails_fixture_file_wrapper
26
+ RailsFixtureFileWrapper.fixture_path = nil
27
+ resolved_fixture_path =
28
+ if respond_to?(:fixture_path) && !fixture_path.nil?
29
+ fixture_path.to_s
30
+ else
31
+ (RSpec.configuration.fixture_path || '').to_s
32
+ end
33
+ RailsFixtureFileWrapper.fixture_path = File.join(resolved_fixture_path, '') unless resolved_fixture_path.strip.empty?
34
+ RailsFixtureFileWrapper.instance
35
+ end
14
36
  end
15
37
 
16
38
  class RailsFixtureFileWrapper
17
39
  include ActionDispatch::TestProcess if defined?(ActionDispatch::TestProcess)
18
40
 
41
+ if ::Rails.version.to_f >= 6.1
42
+ include ActiveSupport::Testing::FileFixtures
43
+ end
44
+
19
45
  class << self
20
- attr_reader :fixture_path
46
+ attr_accessor :fixture_path
21
47
 
22
48
  # Get instance of wrapper
23
49
  def instance
24
50
  @instance ||= new
25
51
  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
52
  end
37
53
  end
38
54
  end
@@ -5,52 +5,54 @@ 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
+ use_transactional_tests && !self.class.uses_transaction?(self)
17
+ end
18
+
12
19
  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
20
+ if RSpec.configuration.use_active_record?
21
+ include Fixtures
23
22
 
24
- self.fixture_path = RSpec.configuration.fixture_path
25
- if ::Rails::VERSION::STRING > '5'
23
+ self.fixture_path = RSpec.configuration.fixture_path
26
24
  self.use_transactional_tests = RSpec.configuration.use_transactional_fixtures
27
- else
28
- self.use_transactional_fixtures = RSpec.configuration.use_transactional_fixtures
25
+ self.use_instantiated_fixtures = RSpec.configuration.use_instantiated_fixtures
26
+
27
+ fixtures RSpec.configuration.global_fixtures if RSpec.configuration.global_fixtures
29
28
  end
30
- self.use_instantiated_fixtures = RSpec.configuration.use_instantiated_fixtures
29
+ end
30
+
31
+ module Fixtures
32
+ extend ActiveSupport::Concern
31
33
 
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)
34
+ class_methods do
35
+ def fixtures(*args)
36
+ orig_methods = private_instance_methods
37
+ super.tap do
38
+ new_methods = private_instance_methods - orig_methods
39
+ new_methods.each do |method_name|
40
+ proxy_method_warning_if_called_in_before_context_scope(method_name)
41
+ end
38
42
  end
39
43
  end
40
- end
41
44
 
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)
45
+ def proxy_method_warning_if_called_in_before_context_scope(method_name)
46
+ orig_implementation = instance_method(method_name)
47
+ define_method(method_name) do |*args, &blk|
48
+ if inspect.include?("before(:context)")
49
+ RSpec.warn_with("Calling fixture method in before :context ")
50
+ else
51
+ orig_implementation.bind(self).call(*args, &blk)
52
+ end
49
53
  end
50
54
  end
51
55
  end
52
-
53
- fixtures RSpec.configuration.global_fixtures if RSpec.configuration.global_fixtures
54
56
  end
55
57
  end
56
58
  end