Skip to content

Commit

Permalink
Add fixture_paths configuration setting
Browse files Browse the repository at this point in the history
  • Loading branch information
jguecaimburu committed Apr 20, 2023
1 parent 1873102 commit afaf749
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 10 deletions.
6 changes: 6 additions & 0 deletions lib/generators/rspec/install/templates/spec/rails_helper.rb
Expand Up @@ -34,7 +34,13 @@
RSpec.configure do |config|
<% if RSpec::Rails::FeatureCheck.has_active_record? -%>
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
<% if ::Rails::VERSION::STRING < '7.1.0' -%>
config.fixture_path = Rails.root.join('spec/fixtures')
<% else -%>
config.fixture_paths = [
Rails.root.join('spec/fixtures')
]
<% end -%>
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
Expand Down
5 changes: 5 additions & 0 deletions lib/rspec/rails/configuration.rb
Expand Up @@ -69,7 +69,12 @@ def self.initialize_configuration(config) # rubocop:disable Metrics/MethodLength
config.add_setting :use_transactional_fixtures, alias_with: :use_transactional_examples
config.add_setting :use_instantiated_fixtures
config.add_setting :global_fixtures

config.add_setting :fixture_paths

# @deprecated TestFixtures#fixture_path is deprecated and will be removed in Rails 7.2
config.add_setting :fixture_path

config.include RSpec::Rails::FixtureSupport, :use_fixtures

# We'll need to create a deprecated module in order to properly report to
Expand Down
11 changes: 7 additions & 4 deletions lib/rspec/rails/fixture_support.rb
Expand Up @@ -22,11 +22,14 @@ def run_in_transaction?
include Fixtures

# TestFixtures#fixture_path is deprecated and will be removed in Rails 7.2
if respond_to?(:fixture_paths=)
fixture_paths << RSpec.configuration.fixture_path
else
self.fixture_path = RSpec.configuration.fixture_path
if respond_to?(:fixture_paths=) && RSpec.configuration.fixture_paths.any?
self.fixture_paths = RSpec.configuration.fixture_paths
elsif respond_to?(:fixture_paths=) && Rspec.configuration.fixture_path
self.fixture_paths = Array(RSpec.configuration.fixture_path)
elsif !respond_to?(:fixture_paths=) && respond_to?(:fixture_path=)
self.fixture_path = RSpec.configuration.fixture_path || RSpec.configuration.fixture_paths&.first
end

self.use_transactional_tests = RSpec.configuration.use_transactional_fixtures
self.use_instantiated_fixtures = RSpec.configuration.use_instantiated_fixtures

Expand Down
11 changes: 10 additions & 1 deletion spec/generators/rspec/install/install_generator_spec.rb
Expand Up @@ -15,6 +15,10 @@ def have_a_fixture_path
match(/^ config\.fixture_path = /m)
end

def have_a_fixture_paths
match(/^ config\.fixture_paths = /m)
end

def maintain_test_schema
match(/ActiveRecord::Migration\.maintain_test_schema!/m)
end
Expand Down Expand Up @@ -84,7 +88,11 @@ def filter_rails_from_backtrace

specify "with default fixture path" do
run_generator
expect(rails_helper).to have_a_fixture_path
if ::Rails::VERSION::STRING < '7.1.0'
expect(rails_helper).to have_a_fixture_path
else
expect(rails_helper).to have_a_fixture_paths
end
end

specify "with transactional fixtures" do
Expand Down Expand Up @@ -127,6 +135,7 @@ def filter_rails_from_backtrace
specify "without fixture path" do
run_generator
expect(rails_helper).not_to have_a_fixture_path
expect(rails_helper).not_to have_a_fixture_paths
end

specify "without transactional fixtures" do
Expand Down
27 changes: 25 additions & 2 deletions spec/rspec/rails/configuration_spec.rb
Expand Up @@ -78,6 +78,9 @@

include_examples "adds setting", :global_fixtures

include_examples "adds setting", :fixture_paths

# @deprecated TestFixtures#fixture_path is deprecated and will be removed in Rails 7.2
include_examples "adds setting", :fixture_path

include_examples "adds setting", :rendering_views
Expand Down Expand Up @@ -157,7 +160,27 @@ def in_inferring_type_from_location_environment
include_examples "infers type from location", :feature, "spec/features"
end

it "fixture support is included with metadata `:use_fixtures`" do
it "fixture support is included with metadata `:use_fixtures` and fixture_paths configured" do
in_sub_process do
RSpec.configuration.global_fixtures = [:foo]
RSpec.configuration.fixture_paths = ["custom/path", "other/custom/path"]

group = RSpec.describe("Arbitrary Description", :use_fixtures)

if ::Rails::VERSION::STRING < '7.1.0'
expect(group).to respond_to(:fixture_path)
expect(group.fixture_path).to eq("custom/path")
else
expect(group).to respond_to(:fixture_paths)
expect(group.fixture_paths).to eq(["custom/path", "other/custom/path"])
end

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

# @deprecated TestFixtures#fixture_path is deprecated and will be removed in Rails 7.2
it "fixture support is included with metadata `:use_fixtures` and fixture_path configured" do
in_sub_process do
RSpec.configuration.global_fixtures = [:foo]
RSpec.configuration.fixture_path = "custom/path"
Expand All @@ -169,7 +192,7 @@ def in_inferring_type_from_location_environment
expect(group.fixture_path).to eq("custom/path")
else
expect(group).to respond_to(:fixture_paths)
expect(group.fixture_paths).to include("custom/path")
expect(group.fixture_paths).to eq(["custom/path"])
end

expect(group.new.respond_to?(:foo, true)).to be(true)
Expand Down
14 changes: 11 additions & 3 deletions spec/rspec/rails/fixture_support_spec.rb
@@ -1,14 +1,22 @@
module RSpec::Rails
RSpec.describe FixtureSupport do
context "with use_transactional_fixtures set to false" do
it "still supports fixture_path" do
it "still supports fixture_path and fixture_paths" do
allow(RSpec.configuration).to receive(:use_transactional_fixtures) { false }
group = RSpec::Core::ExampleGroup.describe do
include FixtureSupport
end

expect(group).to respond_to(:fixture_path)
expect(group).to respond_to(:fixture_path=)
# @deprecated TestFixtures#fixture_path is deprecated and will be removed in Rails 7.2
if ::Rails::VERSION::STRING < '7.1.0'
expect(group).to respond_to(:fixture_path)
expect(group).to respond_to(:fixture_path=)
else
expect(group).to respond_to(:fixture_path)
expect(group).to respond_to(:fixture_path=)
expect(group).to respond_to(:fixture_paths)
expect(group).to respond_to(:fixture_paths=)
end
end
end

Expand Down

0 comments on commit afaf749

Please sign in to comment.