rspec-rails 2.14.2 → 3.9.1

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 (99) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data/.document +1 -1
  4. data/.yardopts +4 -2
  5. data/Capybara.md +2 -4
  6. data/Changelog.md +592 -34
  7. data/{License.txt → LICENSE.md} +5 -2
  8. data/README.md +290 -369
  9. data/lib/generators/rspec/controller/controller_generator.rb +1 -0
  10. data/lib/generators/rspec/controller/templates/controller_spec.rb +5 -5
  11. data/lib/generators/rspec/controller/templates/view_spec.rb +2 -2
  12. data/lib/generators/rspec/feature/feature_generator.rb +29 -0
  13. data/lib/generators/rspec/feature/templates/feature_singular_spec.rb +5 -0
  14. data/lib/generators/rspec/feature/templates/feature_spec.rb +5 -0
  15. data/lib/generators/rspec/generators/generator_generator.rb +24 -0
  16. data/lib/generators/rspec/generators/templates/generator_spec.rb +6 -0
  17. data/lib/generators/rspec/helper/helper_generator.rb +1 -0
  18. data/lib/generators/rspec/helper/templates/helper_spec.rb +2 -2
  19. data/lib/generators/rspec/install/install_generator.rb +44 -5
  20. data/lib/generators/rspec/install/templates/spec/rails_helper.rb +78 -0
  21. data/lib/generators/rspec/integration/integration_generator.rb +8 -13
  22. data/lib/generators/rspec/integration/templates/request_spec.rb +4 -9
  23. data/lib/generators/rspec/job/job_generator.rb +12 -0
  24. data/lib/generators/rspec/job/templates/job_spec.rb.erb +7 -0
  25. data/lib/generators/rspec/mailer/mailer_generator.rb +7 -0
  26. data/lib/generators/rspec/mailer/templates/mailer_spec.rb +7 -7
  27. data/lib/generators/rspec/mailer/templates/preview.rb +13 -0
  28. data/lib/generators/rspec/model/model_generator.rb +19 -5
  29. data/lib/generators/rspec/model/templates/fixtures.yml +1 -1
  30. data/lib/generators/rspec/model/templates/model_spec.rb +2 -2
  31. data/lib/generators/rspec/observer/observer_generator.rb +1 -0
  32. data/lib/generators/rspec/observer/templates/observer_spec.rb +2 -2
  33. data/lib/generators/rspec/request/request_generator.rb +10 -0
  34. data/lib/generators/rspec/scaffold/scaffold_generator.rb +68 -138
  35. data/lib/generators/rspec/scaffold/templates/api_controller_spec.rb +165 -0
  36. data/lib/generators/rspec/scaffold/templates/controller_spec.rb +98 -73
  37. data/lib/generators/rspec/scaffold/templates/edit_spec.rb +9 -13
  38. data/lib/generators/rspec/scaffold/templates/index_spec.rb +3 -10
  39. data/lib/generators/rspec/scaffold/templates/new_spec.rb +10 -14
  40. data/lib/generators/rspec/scaffold/templates/routing_spec.rb +21 -12
  41. data/lib/generators/rspec/scaffold/templates/show_spec.rb +4 -11
  42. data/lib/generators/rspec/system/system_generator.rb +26 -0
  43. data/lib/generators/rspec/system/templates/system_spec.rb +9 -0
  44. data/lib/generators/rspec/view/templates/view_spec.rb +2 -2
  45. data/lib/generators/rspec/view/view_generator.rb +1 -0
  46. data/lib/generators/rspec.rb +20 -6
  47. data/lib/rspec/rails/active_record.rb +25 -0
  48. data/lib/rspec/rails/adapters.rb +104 -37
  49. data/lib/rspec/rails/configuration.rb +148 -0
  50. data/lib/rspec/rails/example/controller_example_group.rb +188 -138
  51. data/lib/rspec/rails/example/feature_example_group.rb +63 -20
  52. data/lib/rspec/rails/example/helper_example_group.rb +35 -26
  53. data/lib/rspec/rails/example/job_example_group.rb +23 -0
  54. data/lib/rspec/rails/example/mailer_example_group.rb +30 -14
  55. data/lib/rspec/rails/example/model_example_group.rb +8 -7
  56. data/lib/rspec/rails/example/rails_example_group.rb +3 -1
  57. data/lib/rspec/rails/example/request_example_group.rb +23 -16
  58. data/lib/rspec/rails/example/routing_example_group.rb +49 -40
  59. data/lib/rspec/rails/example/system_example_group.rb +108 -0
  60. data/lib/rspec/rails/example/view_example_group.rb +168 -135
  61. data/lib/rspec/rails/example.rb +2 -33
  62. data/lib/rspec/rails/extensions/active_record/proxy.rb +0 -1
  63. data/lib/rspec/rails/extensions.rb +0 -1
  64. data/lib/rspec/rails/feature_check.rb +64 -0
  65. data/lib/rspec/rails/file_fixture_support.rb +17 -0
  66. data/lib/rspec/rails/fixture_file_upload_support.rb +40 -0
  67. data/lib/rspec/rails/fixture_support.rb +32 -13
  68. data/lib/rspec/rails/matchers/active_job.rb +317 -0
  69. data/lib/rspec/rails/matchers/base_matcher.rb +184 -0
  70. data/lib/rspec/rails/matchers/be_a_new.rb +69 -62
  71. data/lib/rspec/rails/matchers/be_new_record.rb +24 -21
  72. data/lib/rspec/rails/matchers/be_valid.rb +42 -33
  73. data/lib/rspec/rails/matchers/have_enqueued_mail.rb +174 -0
  74. data/lib/rspec/rails/matchers/have_http_status.rb +381 -0
  75. data/lib/rspec/rails/matchers/have_rendered.rb +54 -31
  76. data/lib/rspec/rails/matchers/redirect_to.rb +30 -29
  77. data/lib/rspec/rails/matchers/relation_match_array.rb +1 -1
  78. data/lib/rspec/rails/matchers/routing_matchers.rb +107 -93
  79. data/lib/rspec/rails/matchers.rb +13 -14
  80. data/lib/rspec/rails/tasks/rspec.rake +1 -1
  81. data/lib/rspec/rails/vendor/capybara.rb +10 -4
  82. data/lib/rspec/rails/version.rb +3 -1
  83. data/lib/rspec/rails/view_assigns.rb +18 -18
  84. data/lib/rspec/rails/view_path_builder.rb +29 -0
  85. data/lib/rspec/rails/view_rendering.rb +89 -63
  86. data/lib/rspec/rails/view_spec_methods.rb +56 -0
  87. data/lib/rspec/rails.rb +10 -10
  88. data/lib/rspec-rails.rb +66 -1
  89. data.tar.gz.sig +0 -0
  90. metadata +92 -77
  91. metadata.gz.sig +0 -0
  92. data/lib/autotest/rails_rspec2.rb +0 -85
  93. data/lib/generators/rspec/install/templates/.rspec +0 -1
  94. data/lib/generators/rspec/install/templates/spec/spec_helper.rb.tt +0 -49
  95. data/lib/rspec/rails/extensions/active_record/base.rb +0 -58
  96. data/lib/rspec/rails/matchers/have_extension.rb +0 -36
  97. data/lib/rspec/rails/mocks.rb +0 -274
  98. data/lib/rspec/rails/module_inclusion.rb +0 -19
  99. data/lib/rspec/rails/vendor/webrat.rb +0 -33
@@ -1,169 +1,202 @@
1
1
  require 'rspec/rails/view_assigns'
2
-
3
- module RSpec::Rails
4
- module ViewExampleGroup
5
- extend ActiveSupport::Concern
6
- include RSpec::Rails::RailsExampleGroup
7
- include ActionView::TestCase::Behavior
8
- include RSpec::Rails::ViewAssigns
9
- include RSpec::Rails::Matchers::RenderTemplate
10
-
11
- module ClassMethods
12
- def _default_helper
13
- base = metadata[:example_group][:description].split('/')[0..-2].join('/')
14
- (base.camelize + 'Helper').constantize if base
15
- rescue NameError
16
- nil
2
+ require 'rspec/rails/view_spec_methods'
3
+ require 'rspec/rails/view_path_builder'
4
+
5
+ module RSpec
6
+ module Rails
7
+ # @api public
8
+ # Container class for view spec functionality.
9
+ module ViewExampleGroup
10
+ extend ActiveSupport::Concern
11
+ include RSpec::Rails::RailsExampleGroup
12
+ include ActionView::TestCase::Behavior
13
+ include RSpec::Rails::ViewAssigns
14
+ include RSpec::Rails::Matchers::RenderTemplate
15
+
16
+ # @private
17
+ module StubResolverCache
18
+ def self.resolver_for(hash)
19
+ @resolvers ||= {}
20
+ @resolvers[hash] ||= ActionView::FixtureResolver.new(hash)
21
+ end
17
22
  end
18
23
 
19
- def _default_helpers
20
- helpers = [_default_helper].compact
21
- helpers << ApplicationHelper if Object.const_defined?('ApplicationHelper')
22
- helpers
23
- end
24
- end
24
+ # @private
25
+ module ClassMethods
26
+ def _default_helper
27
+ base = metadata[:description].split('/')[0..-2].join('/')
28
+ (base.camelize + 'Helper').constantize unless base.to_s.empty?
29
+ rescue NameError
30
+ nil
31
+ end
25
32
 
26
- module ExampleMethods
27
- # @overload render
28
- # @overload render({:partial => path_to_file})
29
- # @overload render({:partial => path_to_file}, {... locals ...})
30
- # @overload render({:partial => path_to_file}, {... locals ...}) do ... end
31
- #
32
- # Delegates to ActionView::Base#render, so see documentation on that
33
- # for more info.
34
- #
35
- # The only addition is that you can call render with no arguments, and RSpec
36
- # will pass the top level description to render:
37
- #
38
- # describe "widgets/new.html.erb" do
39
- # it "shows all the widgets" do
40
- # render # => view.render(:file => "widgets/new.html.erb")
41
- # # ...
42
- # end
43
- # end
44
- def render(options={}, local_assigns={}, &block)
45
- options = _default_render_options if Hash === options and options.empty?
46
- super(options, local_assigns, &block)
33
+ def _default_helpers
34
+ helpers = [_default_helper].compact
35
+ helpers << ApplicationHelper if Object.const_defined?('ApplicationHelper')
36
+ helpers
37
+ end
47
38
  end
48
39
 
49
- # The instance of `ActionView::Base` that is used to render the template.
50
- # Use this to stub methods _before_ calling `render`.
51
- #
52
- # describe "widgets/new.html.erb" do
53
- # it "shows all the widgets" do
54
- # view.stub(:foo) { "foo" }
55
- # render
56
- # # ...
57
- # end
58
- # end
59
- def view
60
- _view
61
- end
40
+ # DSL exposed to view specs.
41
+ module ExampleMethods
42
+ extend ActiveSupport::Concern
62
43
 
63
- # Simulates the presence of a template on the file system by adding a
64
- # Rails' FixtureResolver to the front of the view_paths list. Designed to
65
- # help isolate view examples from partials rendered by the view template
66
- # that is the subject of the example.
67
- #
68
- # @example
69
- #
70
- # stub_template("widgets/_widget.html.erb" => "This content.")
71
- def stub_template(hash)
72
- view.view_paths.unshift(ActionView::FixtureResolver.new(hash))
73
- end
44
+ included do
45
+ include ::Rails.application.routes.url_helpers
74
46
 
75
- # Provides access to the params hash that will be available within the
76
- # view:
77
- #
78
- # params[:foo] = 'bar'
79
- def params
80
- controller.params
81
- end
47
+ if ::Rails.application.routes.respond_to?(:mounted_helpers)
48
+ include ::Rails.application.routes.mounted_helpers
49
+ end
50
+ end
82
51
 
83
- # @deprecated Use `view` instead.
84
- def template
85
- RSpec.deprecate("template", :replacement => "view")
86
- view
87
- end
52
+ # @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
56
+ #
57
+ # Delegates to ActionView::Base#render, so see documentation on that
58
+ # for more info.
59
+ #
60
+ # The only addition is that you can call render with no arguments, and
61
+ # RSpec will pass the top level description to render:
62
+ #
63
+ # describe "widgets/new.html.erb" do
64
+ # it "shows all the widgets" do
65
+ # render # => view.render(:file => "widgets/new.html.erb")
66
+ # # ...
67
+ # end
68
+ # end
69
+ def render(options = {}, local_assigns = {}, &block)
70
+ options = _default_render_options if Hash === options && options.empty?
71
+ super(options, local_assigns, &block)
72
+ end
88
73
 
89
- # @deprecated Use `rendered` instead.
90
- def response
91
- # `assert_template` expects `response` to implement a #body method
92
- # like an `ActionDispatch::Response` does to force the view to render.
93
- # For backwards compatibility, we use #response as an alias for
94
- # #rendered, but it needs to implement #body to avoid `assert_template`
95
- # raising a `NoMethodError`.
96
- unless rendered.respond_to?(:body)
97
- def rendered.body; self; end;
74
+ # The instance of `ActionView::Base` that is used to render the template.
75
+ # Use this to stub methods _before_ calling `render`.
76
+ #
77
+ # describe "widgets/new.html.erb" do
78
+ # it "shows all the widgets" do
79
+ # view.stub(:foo) { "foo" }
80
+ # render
81
+ # # ...
82
+ # end
83
+ # end
84
+ def view
85
+ _view
98
86
  end
99
87
 
100
- rendered
101
- end
88
+ # Simulates the presence of a template on the file system by adding a
89
+ # Rails' FixtureResolver to the front of the view_paths list. Designed to
90
+ # help isolate view examples from partials rendered by the view template
91
+ # that is the subject of the example.
92
+ #
93
+ # stub_template("widgets/_widget.html.erb" => "This content.")
94
+ def stub_template(hash)
95
+ view.view_paths.unshift(StubResolverCache.resolver_for(hash))
96
+ end
102
97
 
103
- private
98
+ # Provides access to the params hash that will be available within the
99
+ # view.
100
+ #
101
+ # params[:foo] = 'bar'
102
+ def params
103
+ controller.params
104
+ end
104
105
 
105
- def _default_file_to_render
106
- example.example_group.top_level_description
107
- end
106
+ # @deprecated Use `view` instead.
107
+ def template
108
+ RSpec.deprecate("template", :replacement => "view")
109
+ view
110
+ end
108
111
 
109
- def _default_render_options
110
- if ::Rails::VERSION::STRING >= '3.2'
111
- # pluck the handler, format, and locale out of, eg, posts/index.de.html.haml
112
- template, *components = _default_file_to_render.split('.')
113
- handler, format, locale = *components.reverse
112
+ # @deprecated Use `rendered` instead.
113
+ def response
114
+ # `assert_template` expects `response` to implement a #body method
115
+ # like an `ActionDispatch::Response` does to force the view to
116
+ # render. For backwards compatibility, we use #response as an alias
117
+ # for #rendered, but it needs to implement #body to avoid
118
+ # `assert_template` raising a `NoMethodError`.
119
+ unless rendered.respond_to?(:body)
120
+ def rendered.body
121
+ self
122
+ end
123
+ end
124
+
125
+ rendered
126
+ end
114
127
 
115
- render_options = {:template => template}
116
- render_options[:handlers] = [handler] if handler
117
- render_options[:formats] = [format] if format
118
- render_options[:locales] = [locale] if locale
128
+ private
119
129
 
120
- render_options
121
- else
122
- {:template => _default_file_to_render}
130
+ 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
123
145
  end
124
- end
125
146
 
126
- def _path_parts
127
- _default_file_to_render.split("/")
128
- end
147
+ def _path_parts
148
+ _default_file_to_render.split("/")
149
+ end
129
150
 
130
- def _controller_path
131
- _path_parts[0..-2].join("/")
132
- end
151
+ def _controller_path
152
+ _path_parts[0..-2].join("/")
153
+ end
133
154
 
134
- def _inferred_action
135
- _path_parts.last.split(".").first
136
- end
155
+ def _inferred_action
156
+ _path_parts.last.split(".").first
157
+ end
137
158
 
138
- def _include_controller_helpers
139
- helpers = controller._helpers
140
- view.singleton_class.class_eval do
141
- include helpers unless included_modules.include?(helpers)
159
+ def _include_controller_helpers
160
+ helpers = controller._helpers
161
+ view.singleton_class.class_exec do
162
+ include helpers unless included_modules.include?(helpers)
163
+ end
142
164
  end
143
165
  end
144
- end
145
166
 
146
- included do
147
- include ExampleMethods
167
+ included do
168
+ include ExampleMethods
169
+
170
+ helper(*_default_helpers)
171
+
172
+ before do
173
+ _include_controller_helpers
174
+ if view.lookup_context.respond_to?(:prefixes)
175
+ # rails 3.1
176
+ view.lookup_context.prefixes << _controller_path
177
+ end
178
+
179
+ controller.controller_path = _controller_path
148
180
 
149
- metadata[:type] = :view
150
- helper(*_default_helpers)
181
+ path_params_to_merge = {}
182
+ path_params_to_merge[:controller] = _controller_path
183
+ path_params_to_merge[:action] = _inferred_action unless _inferred_action =~ /^_/
151
184
 
152
- before do
153
- _include_controller_helpers
154
- if view.lookup_context.respond_to?(:prefixes)
155
- # rails 3.1
156
- view.lookup_context.prefixes << _controller_path
185
+ path_params = controller.request.path_parameters
186
+
187
+ controller.request.path_parameters = path_params.reverse_merge(path_params_to_merge)
188
+ controller.request.path = ViewPathBuilder.new(::Rails.application.routes).path_for(controller.request.path_parameters)
189
+ ViewSpecMethods.add_to(::ActionView::TestCase::TestController)
157
190
  end
158
191
 
159
- # fixes bug with differing formats
160
- view.lookup_context.view_paths.each(&:clear_cache)
192
+ after do
193
+ ViewSpecMethods.remove_from(::ActionView::TestCase::TestController)
194
+ end
161
195
 
162
- controller.controller_path = _controller_path
163
- controller.request.path_parameters[:controller] = _controller_path
164
- controller.request.path_parameters[:action] = _inferred_action unless _inferred_action =~ /^_/
196
+ let(:_default_file_to_render) do |example|
197
+ example.example_group.top_level_description
198
+ end
165
199
  end
166
200
  end
167
201
  end
168
202
  end
169
-
@@ -6,37 +6,6 @@ require 'rspec/rails/example/view_example_group'
6
6
  require 'rspec/rails/example/mailer_example_group'
7
7
  require 'rspec/rails/example/routing_example_group'
8
8
  require 'rspec/rails/example/model_example_group'
9
+ require 'rspec/rails/example/job_example_group'
9
10
  require 'rspec/rails/example/feature_example_group'
10
-
11
- RSpec::configure do |c|
12
- def c.escaped_path(*parts)
13
- Regexp.compile(parts.join('[\\\/]') + '[\\\/]')
14
- end
15
-
16
- c.include RSpec::Rails::ControllerExampleGroup, :type => :controller, :example_group => {
17
- :file_path => c.escaped_path(%w[spec controllers])
18
- }
19
- c.include RSpec::Rails::HelperExampleGroup, :type => :helper, :example_group => {
20
- :file_path => c.escaped_path(%w[spec helpers])
21
- }
22
- if defined?(RSpec::Rails::MailerExampleGroup)
23
- c.include RSpec::Rails::MailerExampleGroup, :type => :mailer, :example_group => {
24
- :file_path => c.escaped_path(%w[spec mailers])
25
- }
26
- end
27
- c.include RSpec::Rails::ModelExampleGroup, :type => :model, :example_group => {
28
- :file_path => c.escaped_path(%w[spec models])
29
- }
30
- c.include RSpec::Rails::RequestExampleGroup, :type => :request, :example_group => {
31
- :file_path => c.escaped_path(%w[spec (requests|integration|api)])
32
- }
33
- c.include RSpec::Rails::RoutingExampleGroup, :type => :routing, :example_group => {
34
- :file_path => c.escaped_path(%w[spec routing])
35
- }
36
- c.include RSpec::Rails::ViewExampleGroup, :type => :view, :example_group => {
37
- :file_path => c.escaped_path(%w[spec views])
38
- }
39
- c.include RSpec::Rails::FeatureExampleGroup, :type => :feature, :example_group => {
40
- :file_path => c.escaped_path(%w[spec features])
41
- }
42
- end
11
+ require 'rspec/rails/example/system_example_group'
@@ -14,4 +14,3 @@ RSpec.configure do |rspec|
14
14
  end
15
15
  end
16
16
  end
17
-
@@ -1,2 +1 @@
1
- require 'rspec/rails/extensions/active_record/base'
2
1
  require 'rspec/rails/extensions/active_record/proxy'
@@ -0,0 +1,64 @@
1
+ module RSpec
2
+ module Rails
3
+ # @private
4
+ # Disable some cops until https://github.com/bbatsov/rubocop/issues/1310
5
+ # rubocop:disable Style/IndentationConsistency
6
+ 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
+
21
+ def has_active_job?
22
+ defined?(::ActiveJob)
23
+ end
24
+
25
+ def has_active_record?
26
+ defined?(::ActiveRecord)
27
+ end
28
+
29
+ def has_active_record_migration?
30
+ has_active_record? && defined?(::ActiveRecord::Migration)
31
+ end
32
+
33
+ def has_action_mailer?
34
+ defined?(::ActionMailer)
35
+ end
36
+
37
+ def has_action_mailer_preview?
38
+ has_action_mailer? && defined?(::ActionMailer::Preview)
39
+ end
40
+
41
+ def has_action_mailer_show_preview?
42
+ has_action_mailer_preview? &&
43
+ ::ActionMailer::Base.respond_to?(:show_previews=)
44
+ end
45
+
46
+ def has_1_9_hash_syntax?
47
+ ::Rails::VERSION::STRING > '4.0'
48
+ end
49
+
50
+ def has_file_fixture?
51
+ ::Rails::VERSION::STRING > '5.0'
52
+ end
53
+
54
+ def type_metatag(type)
55
+ if has_1_9_hash_syntax?
56
+ "type: :#{type}"
57
+ else
58
+ ":type => :#{type}"
59
+ end
60
+ end
61
+ end
62
+ # rubocop:enable Style/IndentationConsistency
63
+ end
64
+ end
@@ -0,0 +1,17 @@
1
+ if ::Rails::VERSION::STRING > '5'
2
+ require 'active_support/testing/file_fixtures'
3
+
4
+ module RSpec
5
+ module Rails
6
+ # @private
7
+ module FileFixtureSupport
8
+ extend ActiveSupport::Concern
9
+ include ActiveSupport::Testing::FileFixtures
10
+
11
+ included do
12
+ self.file_fixture_path = RSpec.configuration.file_fixture_path
13
+ end
14
+ end
15
+ end
16
+ end
17
+ 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
@@ -1,7 +1,8 @@
1
1
  module RSpec
2
2
  module Rails
3
- if defined?(ActiveRecord::TestFixtures)
4
- module FixtureSupport
3
+ # @private
4
+ module FixtureSupport
5
+ if defined?(ActiveRecord::TestFixtures)
5
6
  extend ActiveSupport::Concern
6
7
  include RSpec::Rails::SetupAndTeardownAdapter
7
8
  include RSpec::Rails::MinitestLifecycleAdapter if ::ActiveRecord::VERSION::STRING > '4'
@@ -9,31 +10,49 @@ module RSpec
9
10
  include ActiveRecord::TestFixtures
10
11
 
11
12
  included do
12
- # TODO (DC 2011-06-25) this is necessary because fixture_file_upload
13
+ # TODO: (DC 2011-06-25) this is necessary because fixture_file_upload
13
14
  # accesses fixture_path directly on ActiveSupport::TestCase. This is
14
15
  # fixed in rails by https://github.com/rails/rails/pull/1861, which
15
16
  # should be part of the 3.1 release, at which point we can include
16
17
  # these lines for rails < 3.1.
17
- ActiveSupport::TestCase.class_eval do
18
+ ActiveSupport::TestCase.class_exec do
18
19
  include ActiveRecord::TestFixtures
19
20
  self.fixture_path = RSpec.configuration.fixture_path
20
21
  end
21
22
  # /TODO
22
23
 
23
24
  self.fixture_path = RSpec.configuration.fixture_path
24
- self.use_transactional_fixtures = RSpec.configuration.use_transactional_fixtures
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
29
+ end
25
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)
38
+ end
39
+ end
40
+ end
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)
49
+ end
50
+ end
51
+ end
52
+
26
53
  fixtures RSpec.configuration.global_fixtures if RSpec.configuration.global_fixtures
27
54
  end
28
55
  end
29
-
30
- RSpec.configure do |c|
31
- c.include RSpec::Rails::FixtureSupport
32
- c.add_setting :use_transactional_fixtures, :alias_with => :use_transactional_examples
33
- c.add_setting :use_instantiated_fixtures
34
- c.add_setting :global_fixtures
35
- c.add_setting :fixture_path
36
- end
37
56
  end
38
57
  end
39
58
  end