Skip to content

Commit afaf749

Browse files
committed
Add fixture_paths configuration setting
1 parent 1873102 commit afaf749

File tree

6 files changed

+64
-10
lines changed

6 files changed

+64
-10
lines changed

lib/generators/rspec/install/templates/spec/rails_helper.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@
3434
RSpec.configure do |config|
3535
<% if RSpec::Rails::FeatureCheck.has_active_record? -%>
3636
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
37+
<% if ::Rails::VERSION::STRING < '7.1.0' -%>
3738
config.fixture_path = Rails.root.join('spec/fixtures')
39+
<% else -%>
40+
config.fixture_paths = [
41+
Rails.root.join('spec/fixtures')
42+
]
43+
<% end -%>
3844
3945
# If you're not using ActiveRecord, or you'd prefer not to run each of your
4046
# examples within a transaction, remove the following line or assign false

lib/rspec/rails/configuration.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@ def self.initialize_configuration(config) # rubocop:disable Metrics/MethodLength
6969
config.add_setting :use_transactional_fixtures, alias_with: :use_transactional_examples
7070
config.add_setting :use_instantiated_fixtures
7171
config.add_setting :global_fixtures
72+
73+
config.add_setting :fixture_paths
74+
75+
# @deprecated TestFixtures#fixture_path is deprecated and will be removed in Rails 7.2
7276
config.add_setting :fixture_path
77+
7378
config.include RSpec::Rails::FixtureSupport, :use_fixtures
7479

7580
# We'll need to create a deprecated module in order to properly report to

lib/rspec/rails/fixture_support.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ def run_in_transaction?
2222
include Fixtures
2323

2424
# TestFixtures#fixture_path is deprecated and will be removed in Rails 7.2
25-
if respond_to?(:fixture_paths=)
26-
fixture_paths << RSpec.configuration.fixture_path
27-
else
28-
self.fixture_path = RSpec.configuration.fixture_path
25+
if respond_to?(:fixture_paths=) && RSpec.configuration.fixture_paths.any?
26+
self.fixture_paths = RSpec.configuration.fixture_paths
27+
elsif respond_to?(:fixture_paths=) && Rspec.configuration.fixture_path
28+
self.fixture_paths = Array(RSpec.configuration.fixture_path)
29+
elsif !respond_to?(:fixture_paths=) && respond_to?(:fixture_path=)
30+
self.fixture_path = RSpec.configuration.fixture_path || RSpec.configuration.fixture_paths&.first
2931
end
32+
3033
self.use_transactional_tests = RSpec.configuration.use_transactional_fixtures
3134
self.use_instantiated_fixtures = RSpec.configuration.use_instantiated_fixtures
3235

spec/generators/rspec/install/install_generator_spec.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ def have_a_fixture_path
1515
match(/^ config\.fixture_path = /m)
1616
end
1717

18+
def have_a_fixture_paths
19+
match(/^ config\.fixture_paths = /m)
20+
end
21+
1822
def maintain_test_schema
1923
match(/ActiveRecord::Migration\.maintain_test_schema!/m)
2024
end
@@ -84,7 +88,11 @@ def filter_rails_from_backtrace
8488

8589
specify "with default fixture path" do
8690
run_generator
87-
expect(rails_helper).to have_a_fixture_path
91+
if ::Rails::VERSION::STRING < '7.1.0'
92+
expect(rails_helper).to have_a_fixture_path
93+
else
94+
expect(rails_helper).to have_a_fixture_paths
95+
end
8896
end
8997

9098
specify "with transactional fixtures" do
@@ -127,6 +135,7 @@ def filter_rails_from_backtrace
127135
specify "without fixture path" do
128136
run_generator
129137
expect(rails_helper).not_to have_a_fixture_path
138+
expect(rails_helper).not_to have_a_fixture_paths
130139
end
131140

132141
specify "without transactional fixtures" do

spec/rspec/rails/configuration_spec.rb

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@
7878

7979
include_examples "adds setting", :global_fixtures
8080

81+
include_examples "adds setting", :fixture_paths
82+
83+
# @deprecated TestFixtures#fixture_path is deprecated and will be removed in Rails 7.2
8184
include_examples "adds setting", :fixture_path
8285

8386
include_examples "adds setting", :rendering_views
@@ -157,7 +160,27 @@ def in_inferring_type_from_location_environment
157160
include_examples "infers type from location", :feature, "spec/features"
158161
end
159162

160-
it "fixture support is included with metadata `:use_fixtures`" do
163+
it "fixture support is included with metadata `:use_fixtures` and fixture_paths configured" do
164+
in_sub_process do
165+
RSpec.configuration.global_fixtures = [:foo]
166+
RSpec.configuration.fixture_paths = ["custom/path", "other/custom/path"]
167+
168+
group = RSpec.describe("Arbitrary Description", :use_fixtures)
169+
170+
if ::Rails::VERSION::STRING < '7.1.0'
171+
expect(group).to respond_to(:fixture_path)
172+
expect(group.fixture_path).to eq("custom/path")
173+
else
174+
expect(group).to respond_to(:fixture_paths)
175+
expect(group.fixture_paths).to eq(["custom/path", "other/custom/path"])
176+
end
177+
178+
expect(group.new.respond_to?(:foo, true)).to be(true)
179+
end
180+
end
181+
182+
# @deprecated TestFixtures#fixture_path is deprecated and will be removed in Rails 7.2
183+
it "fixture support is included with metadata `:use_fixtures` and fixture_path configured" do
161184
in_sub_process do
162185
RSpec.configuration.global_fixtures = [:foo]
163186
RSpec.configuration.fixture_path = "custom/path"
@@ -169,7 +192,7 @@ def in_inferring_type_from_location_environment
169192
expect(group.fixture_path).to eq("custom/path")
170193
else
171194
expect(group).to respond_to(:fixture_paths)
172-
expect(group.fixture_paths).to include("custom/path")
195+
expect(group.fixture_paths).to eq(["custom/path"])
173196
end
174197

175198
expect(group.new.respond_to?(:foo, true)).to be(true)

spec/rspec/rails/fixture_support_spec.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
module RSpec::Rails
22
RSpec.describe FixtureSupport do
33
context "with use_transactional_fixtures set to false" do
4-
it "still supports fixture_path" do
4+
it "still supports fixture_path and fixture_paths" do
55
allow(RSpec.configuration).to receive(:use_transactional_fixtures) { false }
66
group = RSpec::Core::ExampleGroup.describe do
77
include FixtureSupport
88
end
99

10-
expect(group).to respond_to(:fixture_path)
11-
expect(group).to respond_to(:fixture_path=)
10+
# @deprecated TestFixtures#fixture_path is deprecated and will be removed in Rails 7.2
11+
if ::Rails::VERSION::STRING < '7.1.0'
12+
expect(group).to respond_to(:fixture_path)
13+
expect(group).to respond_to(:fixture_path=)
14+
else
15+
expect(group).to respond_to(:fixture_path)
16+
expect(group).to respond_to(:fixture_path=)
17+
expect(group).to respond_to(:fixture_paths)
18+
expect(group).to respond_to(:fixture_paths=)
19+
end
1220
end
1321
end
1422

0 commit comments

Comments
 (0)