Skip to content

Commit 8aad725

Browse files
committed
avoid include active record test fixtures if use_active_record = false`
1 parent ef12dbc commit 8aad725

File tree

3 files changed

+56
-53
lines changed

3 files changed

+56
-53
lines changed

example_app_generator/no_active_record/spec/verify_no_fixture_setup_spec.rb

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
1-
# Pretend that ActiveRecord::Rails is defined and this doesn't blow up
2-
# with `config.use_active_record = false`.
3-
# Trick the other spec that checks that ActiveRecord is
4-
# *not* defined by wrapping it in RSpec::Rails namespace
5-
# so that it's reachable from RSpec::Rails::FixtureSupport.
6-
# NOTE: this has to be defined before requiring `rails_helper`.
7-
module RSpec
8-
module Rails
9-
module ActiveRecord
10-
module TestFixtures
11-
end
12-
end
13-
end
14-
end
15-
161
require 'rails_helper'
172

18-
RSpec.describe 'Example App', :use_fixtures, type: :model do
3+
RSpec.describe 'Example App', type: :model do
194
it "does not set up fixtures" do
205
expect(defined?(fixtures)).not_to be
216
end

lib/rspec/rails/fixture_support.rb

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,50 @@ module RSpec
22
module Rails
33
# @private
44
module FixtureSupport
5-
if defined?(ActiveRecord::TestFixtures)
6-
extend ActiveSupport::Concern
7-
include RSpec::Rails::SetupAndTeardownAdapter
8-
include RSpec::Rails::MinitestLifecycleAdapter
9-
include RSpec::Rails::MinitestAssertionAdapter
10-
include ActiveRecord::TestFixtures
11-
12-
# @private prevent ActiveSupport::TestFixtures to start a DB transaction.
13-
# Monkey patched to avoid collisions with 'let(:name)' since Rails 6.1
14-
def run_in_transaction?
15-
current_example_name = (RSpec.current_example && RSpec.current_example.metadata[:description])
16-
use_transactional_tests && !self.class.uses_transaction?(current_example_name)
17-
end
18-
19-
included do
20-
if RSpec.configuration.use_active_record?
21-
include Fixtures
5+
extend ActiveSupport::Concern
6+
7+
included do
8+
if RSpec.configuration.use_active_record? && defined?(ActiveRecord::TestFixtures)
9+
include RSpec::Rails::SetupAndTeardownAdapter
10+
include RSpec::Rails::MinitestLifecycleAdapter
11+
include RSpec::Rails::MinitestAssertionAdapter
12+
include ActiveRecord::TestFixtures
13+
include Fixtures
14+
15+
# @private prevent ActiveSupport::TestFixtures to start a DB transaction.
16+
# Monkey patched to avoid collisions with 'let(:name)' since Rails 6.1
17+
def run_in_transaction?
18+
current_example_name = (RSpec.current_example && RSpec.current_example.metadata[:description])
19+
use_transactional_tests && !self.class.uses_transaction?(current_example_name)
20+
end
2221

23-
self.fixture_paths = RSpec.configuration.fixture_paths
22+
self.fixture_paths = RSpec.configuration.fixture_paths
2423

25-
self.use_transactional_tests = RSpec.configuration.use_transactional_fixtures
26-
self.use_instantiated_fixtures = RSpec.configuration.use_instantiated_fixtures
24+
self.use_transactional_tests = RSpec.configuration.use_transactional_fixtures
25+
self.use_instantiated_fixtures = RSpec.configuration.use_instantiated_fixtures
2726

28-
fixtures RSpec.configuration.global_fixtures if RSpec.configuration.global_fixtures
29-
end
27+
fixtures RSpec.configuration.global_fixtures if RSpec.configuration.global_fixtures
3028
end
29+
end
3130

32-
module Fixtures
33-
extend ActiveSupport::Concern
31+
module Fixtures
32+
extend ActiveSupport::Concern
3433

35-
class_methods do
36-
def fixtures(*args)
37-
super.tap do
38-
fixture_sets.each_pair do |method_name, fixture_name|
39-
proxy_method_warning_if_called_in_before_context_scope(method_name, fixture_name)
40-
end
34+
class_methods do
35+
def fixtures(*args)
36+
super.tap do
37+
fixture_sets.each_pair do |method_name, fixture_name|
38+
proxy_method_warning_if_called_in_before_context_scope(method_name, fixture_name)
4139
end
4240
end
41+
end
4342

44-
def proxy_method_warning_if_called_in_before_context_scope(method_name, fixture_name)
45-
define_method(method_name) do |*args, **kwargs, &blk|
46-
if RSpec.current_scope == :before_context_hook
47-
RSpec.warn_with("Calling fixture method in before :context ")
48-
else
49-
access_fixture(fixture_name, *args, **kwargs, &blk)
50-
end
43+
def proxy_method_warning_if_called_in_before_context_scope(method_name, fixture_name)
44+
define_method(method_name) do |*args, **kwargs, &blk|
45+
if RSpec.current_scope == :before_context_hook
46+
RSpec.warn_with("Calling fixture method in before :context ")
47+
else
48+
access_fixture(fixture_name, *args, **kwargs, &blk)
5149
end
5250
end
5351
end

spec/rspec/rails/fixture_support_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,25 @@ def expect_to_pass(group)
6969
failure_reporter.exceptions.map { |e| raise e }
7070
expect(result).to be true
7171
end
72+
73+
context "with use_active_record set to false" do
74+
it "does not support fixture_path/fixture_paths" do
75+
allow(RSpec.configuration).to receive(:use_active_record) { false }
76+
group = RSpec::Core::ExampleGroup.describe do
77+
include FixtureSupport
78+
end
79+
80+
expect(group).not_to respond_to(:fixture_paths)
81+
end
82+
83+
it "does not include ActiveRecord::TestFixtures" do
84+
allow(RSpec.configuration).to receive(:use_active_record) { false }
85+
group = RSpec::Core::ExampleGroup.describe do
86+
include FixtureSupport
87+
end
88+
89+
expect(group).not_to include(ActiveRecord::TestFixtures)
90+
end
91+
end
7292
end
7393
end

0 commit comments

Comments
 (0)