Skip to content

Commit 479d2d0

Browse files
committed
Merge pull request #1372 from rspec/update-fixture-support
Update fixture support
2 parents 192da8a + 278972d commit 479d2d0

File tree

8 files changed

+251
-14
lines changed

8 files changed

+251
-14
lines changed

.rspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
--color
21
--warnings
2+
--color
3+
--require spec_helper

Changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Enhancements:
88
* Add support for `ActiveJob` specs as standard `RSpec::Rails::RailsExampleGoup`s
99
via both `:type => :job` and inferring type from spec directory `spec/jobs`.
1010
(Gabe Martin-Dempesy, #1361)
11+
* Include `RSpec::Rails::FixtureSupport` into example groups using metadata
12+
`:use_fixtures => true`. (Aaron Kromer, #1372)
1113

1214
Bug Fixes:
1315

@@ -16,6 +18,8 @@ Bug Fixes:
1618
* Remove pre-loading of ActionMailer in the Railtie (Aaron Kromer, #1327)
1719
* Fix undefined method `need_auto_run=` error when using Ruby 2.1 and Rails 3.2
1820
without the test-unit gem (Orien Madgwick, #1350)
21+
* Fix load order issued which causes an undefined method `fixture_path` error
22+
when loading rspec-rails after a spec has been created. (Aaron Kromer, #1372)
1923

2024
### 3.2.1 / 2015-02-23
2125
[Full Changelog](http://github.com/rspec/rspec-rails/compare/v3.2.0...v3.2.1)

example_app_generator/generate_stuff.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ def using_source_path(path)
106106

107107
# Use the absolute path so we can load it without active record too
108108
apply File.join(DEFAULT_SOURCE_PATH, 'generate_action_mailer_specs.rb')
109+
using_source_path(File.expand_path('..', __FILE__)) do
110+
# rspec-core loads files alphabetically, so we want this to be the first one
111+
copy_file 'spec/__verify_fixture_load_order_spec.rb'
112+
end
109113

110114
gsub_file 'spec/spec_helper.rb', /^=(begin|end)/, ''
111115

example_app_generator/run_specs.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
run('bin/rspec spec -cfdoc') || abort
2+
# Ensure we test the issue in-case this isn't the first spec file loaded
3+
run(
4+
'bin/rspec --backtrace -cfdoc spec/__verify_fixture_load_order_spec.rb'
5+
) || abort
26
run('bin/rake --backtrace spec') || abort
37
run('bin/rake --backtrace spec:requests') || abort
48
run('bin/rake --backtrace spec:models') || abort
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This spec needs to be run before `rails_helper` is loaded to check the issue
2+
RSpec.describe "Verify issue rspec/rspec-rails#1355" do
3+
it "passes" do
4+
expect(1).to eq 1
5+
end
6+
end
7+
require 'rails_helper'

lib/rspec/rails/configuration.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,20 @@ def self.initialize_configuration(config)
6161
config.add_setting :infer_base_class_for_anonymous_controllers, :default => true
6262

6363
# fixture support
64-
config.include RSpec::Rails::FixtureSupport
6564
config.add_setting :use_transactional_fixtures, :alias_with => :use_transactional_examples
6665
config.add_setting :use_instantiated_fixtures
6766
config.add_setting :global_fixtures
6867
config.add_setting :fixture_path
68+
config.include RSpec::Rails::FixtureSupport, :use_fixtures
69+
70+
# We'll need to create a deprecated module in order to properly report to
71+
# gems / projects which are relying on this being loaded globally.
72+
#
73+
# See rspec/rspec-rails#1355 for history
74+
#
75+
# @deprecated Include `RSpec::Rails::RailsExampleGroup` or
76+
# `RSpec::Rails::FixtureSupport` directly instead
77+
config.include RSpec::Rails::FixtureSupport
6978

7079
# This allows us to expose `render_views` as a config option even though it
7180
# breaks the convention of other options by using `render_views` as a

lib/rspec/rails/example/rails_example_group.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module RailsExampleGroup
1111
include RSpec::Rails::MinitestLifecycleAdapter if ::Rails::VERSION::STRING >= '4'
1212
include RSpec::Rails::MinitestAssertionAdapter
1313
include RSpec::Rails::Matchers
14+
include RSpec::Rails::FixtureSupport
1415
end
1516
end
1617
end
Lines changed: 219 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,233 @@
11
require "spec_helper"
2+
require 'rspec/support/spec/in_sub_process'
3+
4+
RSpec.describe "Configuration" do
5+
6+
include RSpec::Support::InSubProcess
7+
8+
subject(:config) { RSpec::Core::Configuration.new }
29

3-
describe "configuration" do
410
before do
5-
@orig_render_views = RSpec.configuration.render_views?
11+
RSpec::Rails.initialize_configuration(config)
12+
end
13+
14+
it "adds 'vendor/' to the backtrace exclusions" do
15+
expect(config.backtrace_exclusion_patterns).to include(/vendor\//)
16+
end
17+
18+
it "adds 'lib/rspec/rails' to the backtrace exclusions" do
19+
expect(
20+
config.backtrace_exclusion_patterns
21+
).to include(%r{ lib/rspec/rails })
22+
end
23+
24+
shared_examples_for "adds setting" do |accessor, opts|
25+
opts ||= {}
26+
default_value = opts[:default]
27+
alias_setting = opts[:alias_with]
28+
query_method = "#{accessor}?".to_sym
29+
command_method = "#{accessor}=".to_sym
30+
31+
specify "`##{query_method}` is `#{default_value.inspect}` by default" do
32+
expect(config.send(query_method)).to be(default_value)
33+
end
34+
35+
describe "`##{command_method}`" do
36+
it "changes `#{query_method}` to the provided value" do
37+
expect {
38+
config.send(command_method, :a_value)
39+
}.to change { config.send(query_method) }.to(:a_value)
40+
end
41+
42+
it "sets `#{accessor}` to the provided value" do
43+
expect {
44+
config.send(command_method, :a_value)
45+
}.to change {
46+
config.send(accessor)
47+
}.from(default_value).to(:a_value)
48+
end
49+
end
50+
51+
if alias_setting
52+
specify "`##{alias_setting}` is an alias for `#{accessor}`" do
53+
expect {
54+
config.send(command_method, :a_value)
55+
}.to change { config.send(alias_setting) }.to(:a_value)
56+
end
57+
end
658
end
759

8-
after do
9-
RSpec.configuration.render_views = @orig_render_views
60+
context "adds settings" do
61+
include_examples "adds setting",
62+
:infer_base_class_for_anonymous_controllers,
63+
:default => true
64+
65+
include_examples "adds setting",
66+
:use_transactional_fixtures,
67+
:alias_with => :use_transactional_examples
68+
69+
include_examples "adds setting", :use_instantiated_fixtures
70+
71+
include_examples "adds setting", :global_fixtures
72+
73+
include_examples "adds setting", :fixture_path
74+
75+
include_examples "adds setting", :rendering_views
76+
77+
specify "`#render_views?` is falsey by default" do
78+
expect(config.render_views?).to be_falsey
79+
end
80+
81+
specify "`#render_views` sets `render_views?` to `true`" do
82+
expect {
83+
config.render_views
84+
}.to change { config.render_views? }.to be(true)
85+
end
86+
87+
describe "`#render_views=`" do
88+
it "sets `render_views?` to the provided value" do
89+
expect {
90+
config.render_views = false
91+
}.to change { config.render_views? }.from(nil).to(false)
92+
end
93+
94+
it "sets `render_views` to the provided value" do
95+
expect {
96+
config.render_views = :a_value
97+
}.to change { config.render_views? }.to(:a_value)
98+
end
99+
end
100+
end
101+
102+
describe "#infer_spec_type_from_file_location!" do
103+
def in_inferring_type_from_location_environment
104+
in_sub_process do
105+
RSpec.configuration.infer_spec_type_from_file_location!
106+
yield
107+
end
108+
end
109+
110+
shared_examples_for "infers type from location" do |type, location|
111+
it "sets the type to `#{type.inspect}` for file path `#{location}`" do
112+
in_inferring_type_from_location_environment do
113+
allow(RSpec::Core::Metadata).to receive(:relative_path).and_return(
114+
"./#{location}/foos_spec.rb"
115+
)
116+
group = RSpec.describe("Arbitrary Description")
117+
expect(group.metadata).to include(:type => type)
118+
end
119+
end
120+
end
121+
122+
include_examples "infers type from location",
123+
:controller,
124+
"spec/controllers"
125+
include_examples "infers type from location", :helper, "spec/helpers"
126+
include_examples "infers type from location", :mailer, "spec/mailers"
127+
include_examples "infers type from location", :model, "spec/models"
128+
include_examples "infers type from location", :request, "spec/requests"
129+
include_examples "infers type from location", :request, "spec/integration"
130+
include_examples "infers type from location", :request, "spec/api"
131+
include_examples "infers type from location", :routing, "spec/routing"
132+
include_examples "infers type from location", :view, "spec/views"
133+
include_examples "infers type from location", :feature, "spec/features"
10134
end
11135

12-
describe "#render_views?" do
13-
it "is false by default" do
14-
expect(RSpec.configuration.render_views?).to be_falsey
136+
it "fixture support is included with metadata `:use_fixtures`" do
137+
in_sub_process do
138+
RSpec.configuration.global_fixtures = [:foo]
139+
RSpec.configuration.fixture_path = "custom/path"
140+
141+
group = RSpec.describe("Arbitrary Description", :use_fixtures)
142+
143+
expect(group).to respond_to(:fixture_path)
144+
expect(group.fixture_path).to eq("custom/path")
145+
expect(group.new.respond_to?(:foo, true)).to be(true)
15146
end
16147
end
17148

18-
describe "#render_views" do
19-
it "sets render_views? to return true" do
20-
RSpec.configuration.render_views = false
21-
RSpec.configuration.render_views
149+
it "metadata `:type => :controller` sets up controller example groups" do
150+
a_controller_class = Class.new
151+
stub_const "SomeController", a_controller_class
152+
153+
group = RSpec.describe(SomeController, :type => :controller)
154+
155+
expect(group.controller_class).to be(a_controller_class)
156+
expect(group.new).to be_a(RSpec::Rails::ControllerExampleGroup)
157+
end
158+
159+
it "metadata `:type => :helper` sets up helper example groups" do
160+
a_helper_module = Module.new
161+
stub_const "SomeHelper", a_helper_module
162+
163+
group = RSpec.describe(SomeHelper, :type => :helper)
164+
165+
expect(
166+
group.determine_default_helper_class(:ignored)
167+
).to be(a_helper_module)
168+
expect(group.new).to be_a(RSpec::Rails::HelperExampleGroup)
169+
end
170+
171+
it "metadata `:type => :model` sets up model example groups" do
172+
a_model_class = Class.new
173+
stub_const "SomeModel", a_model_class
174+
175+
group = RSpec.describe(SomeModel, :type => :model)
176+
177+
expect(group.new).to be_a(RSpec::Rails::ModelExampleGroup)
178+
end
179+
180+
it "metadata `:type => :request` sets up request example groups" do
181+
a_rails_app = double("Rails application")
182+
the_rails_module = double("Rails module", :application => a_rails_app)
183+
stub_const "Rails", the_rails_module
22184

23-
expect(RSpec.configuration.render_views?).to be_truthy
185+
group = RSpec.describe("Some Request API", :type => :request)
186+
187+
expect(group.new.app).to be(a_rails_app)
188+
expect(group.new).to be_a(RSpec::Rails::RequestExampleGroup)
189+
end
190+
191+
it "metadata `:type => :routing` sets up routing example groups" do
192+
a_controller_class = Class.new
193+
stub_const "SomeController", a_controller_class
194+
195+
group = RSpec.describe(SomeController, :type => :routing)
196+
197+
expect(group).to respond_to(:routes)
198+
expect(group.new).to be_a(RSpec::Rails::RoutingExampleGroup)
199+
end
200+
201+
it "metadata `:type => :view` sets up view example groups" do
202+
a_helper_module = Module.new
203+
stub_const "SomeControllerHelper", a_helper_module
204+
205+
group = RSpec.describe("some_controller/action.html.erb", :type => :view)
206+
207+
expect(group._default_helper).to be(a_helper_module)
208+
expect(group.new).to be_a(RSpec::Rails::ViewExampleGroup)
209+
end
210+
211+
it "metadata `:type => :feature` sets up feature example groups" do
212+
a_rails_app = double("Rails application")
213+
the_rails_module = double("Rails module", :application => a_rails_app)
214+
stub_const "Rails", the_rails_module
215+
216+
group = RSpec.describe("Some feature description", :type => :feature)
217+
example = group.new
218+
219+
expect(example).to respond_to(:visit)
220+
expect(example).to be_a(RSpec::Rails::FeatureExampleGroup)
221+
end
222+
223+
if defined?(ActionMailer)
224+
it "metadata `:type => :mailer` sets up mailer example groups" do
225+
a_mailer_class = Class.new
226+
stub_const "SomeMailer", a_mailer_class
227+
group = RSpec.describe(SomeMailer, :type => :mailer)
228+
expect(group.mailer_class).to be(a_mailer_class)
229+
expect(group.new).to be_a(RSpec::Rails::MailerExampleGroup)
24230
end
25231
end
232+
26233
end

0 commit comments

Comments
 (0)