Skip to content

Commit 40e9fef

Browse files
committed
Merge pull request #2266 from rspec/configure-active-record
Add configuration to surpress active_record checks
1 parent dd068ac commit 40e9fef

File tree

7 files changed

+103
-35
lines changed

7 files changed

+103
-35
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Enhancements:
66
* Allow `ActiveJob` matchers `#on_queue` modifier to take symbolic queue names. (Nils Sommer, #2283)
77
* The scaffold generator now generates request specs in preference to controller specs.
88
(Luka Lüdicke, #2288)
9+
* Add configuration option to disable ActiveRecord. (Jon Rowe, Phil Pirozhkov, Hermann Mayer, #2266)
910

1011
Bug Fixes:
1112

example_app_generator/generate_stuff.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def setup_tasks
4444

4545
def final_tasks
4646
copy_file 'spec/verify_no_active_record_spec.rb'
47+
copy_file 'spec/verify_no_fixture_setup_spec.rb'
4748
end
4849

4950
def skip_active_record?
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
16+
require 'rails_helper'
17+
18+
RSpec.describe 'Example App', :use_fixtures, type: :model do
19+
it "does not set up fixtures" do
20+
expect(defined?(fixtures)).not_to be
21+
end
22+
end

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@
4242
# instead of true.
4343
config.use_transactional_fixtures = true
4444
45+
# You can uncomment this line to turn off ActiveRecord support entirely.
46+
# config.use_active_record = false
47+
48+
<% else -%>
49+
# Remove this line to enable support for ActiveRecord
50+
config.use_active_record = false
51+
52+
# If you enable ActiveRecord support you should unncomment these lines,
53+
# note if you'd prefer not to run each example within a transaction, you
54+
# should set use_transactional_fixtures to false.
55+
#
56+
# config.fixture_path = "#{::Rails.root}/spec/fixtures"
57+
# config.use_transactional_fixtures = true
58+
4559
<% end -%>
4660
# RSpec Rails can automatically mix in different behaviours to your tests
4761
# based on their file location, for example enabling you to call `get` and

lib/rspec/rails/configuration.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def self.initialize_configuration(config) # rubocop:disable Metrics/MethodLength
6363
config.add_setting :infer_base_class_for_anonymous_controllers, default: true
6464

6565
# fixture support
66+
config.add_setting :use_active_record, default: true
6667
config.add_setting :use_transactional_fixtures, alias_with: :use_transactional_examples
6768
config.add_setting :use_instantiated_fixtures
6869
config.add_setting :global_fixtures

lib/rspec/rails/fixture_support.rb

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,53 @@ module FixtureSupport
1010
include ActiveRecord::TestFixtures
1111

1212
included do
13-
self.fixture_path = RSpec.configuration.fixture_path
14-
if ::Rails::VERSION::STRING > '5'
15-
self.use_transactional_tests = RSpec.configuration.use_transactional_fixtures
16-
else
17-
self.use_transactional_fixtures = RSpec.configuration.use_transactional_fixtures
13+
if RSpec.configuration.use_active_record?
14+
include Fixtures
15+
16+
self.fixture_path = RSpec.configuration.fixture_path
17+
if ::Rails::VERSION::STRING > '5'
18+
self.use_transactional_tests = RSpec.configuration.use_transactional_fixtures
19+
else
20+
self.use_transactional_fixtures = RSpec.configuration.use_transactional_fixtures
21+
end
22+
self.use_instantiated_fixtures = RSpec.configuration.use_instantiated_fixtures
23+
24+
fixtures RSpec.configuration.global_fixtures if RSpec.configuration.global_fixtures
1825
end
19-
self.use_instantiated_fixtures = RSpec.configuration.use_instantiated_fixtures
26+
end
2027

21-
def self.fixtures(*args)
22-
orig_methods = private_instance_methods
23-
super.tap do
24-
new_methods = private_instance_methods - orig_methods
25-
new_methods.each do |method_name|
26-
proxy_method_warning_if_called_in_before_context_scope(method_name)
28+
module Fixtures
29+
extend ActiveSupport::Concern
30+
31+
class_methods do
32+
def 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
2739
end
2840
end
29-
end
3041

31-
def self.proxy_method_warning_if_called_in_before_context_scope(method_name)
32-
orig_implementation = instance_method(method_name)
33-
define_method(method_name) do |*args, &blk|
34-
if inspect.include?("before(:context)")
35-
RSpec.warn_with("Calling fixture method in before :context ")
36-
else
37-
orig_implementation.bind(self).call(*args, &blk)
42+
def 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
3850
end
3951
end
4052
end
4153

4254
if ::Rails.version.to_f >= 6.1
55+
# @private return the example name for TestFixtures
4356
def name
4457
@example
4558
end
4659
end
47-
48-
fixtures RSpec.configuration.global_fixtures if RSpec.configuration.global_fixtures
4960
end
5061
end
5162
end

spec/generators/rspec/install/install_generator_spec.rb

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def content_for(file_name)
1212
end
1313

1414
def have_a_fixture_path
15-
match(/config\.fixture_path/m)
15+
match(/^ config\.fixture_path = /m)
1616
end
1717

1818
def maintain_test_schema
@@ -23,8 +23,16 @@ def require_rspec_rails
2323
match(/^require 'rspec\/rails'$/m)
2424
end
2525

26-
def have_transactional_fixtures_enabled?
27-
match(/config\.use_transactional_fixtures/m)
26+
def have_active_record_enabled
27+
match(/^\ # config\.use_active_record = false/m)
28+
end
29+
30+
def have_active_record_disabled
31+
match(/^\ config\.use_active_record = false/m)
32+
end
33+
34+
def have_transactional_fixtures_enabled
35+
match(/^ config\.use_transactional_fixtures = true/m)
2836
end
2937

3038
def filter_rails_from_backtrace
@@ -59,19 +67,24 @@ def filter_rails_from_backtrace
5967
expect(rails_helper).to require_rspec_rails
6068
end
6169

62-
specify "with transactional fixtures" do
70+
specify "with ActiveRecord" do
6371
run_generator
64-
expect(rails_helper).to have_a_fixture_path
72+
expect(rails_helper).to have_active_record_enabled
73+
expect(rails_helper).not_to have_active_record_disabled
6574
end
6675

6776
specify "with default fixture path" do
6877
run_generator
69-
expect(rails_helper).to have_transactional_fixtures_enabled?
78+
expect(rails_helper).to have_a_fixture_path
7079
end
7180

72-
specify "excluding rails gems from the backtrace" do
81+
specify "with transactional fixtures" do
7382
run_generator
83+
expect(rails_helper).to have_transactional_fixtures_enabled
84+
end
7485

86+
specify "excluding rails gems from the backtrace" do
87+
run_generator
7588
expect(rails_helper).to filter_rails_from_backtrace
7689
end
7790

@@ -86,26 +99,31 @@ def filter_rails_from_backtrace
8699
hide_const("ActiveRecord")
87100
end
88101

89-
it "requires rspec/rails" do
102+
specify "requiring rspec/rails" do
90103
run_generator
91104
expect(rails_helper).to require_rspec_rails
92105
end
93106

94-
it "does not include config.fixture_path" do
107+
specify "without ActiveRecord" do
108+
run_generator
109+
expect(rails_helper).not_to have_active_record_enabled
110+
expect(rails_helper).to have_active_record_disabled
111+
end
112+
113+
specify "without fixture path" do
95114
run_generator
96115
expect(rails_helper).not_to have_a_fixture_path
97116
end
98117

99-
it "does not include config.use_transactional_fixtures" do
118+
specify "without transactional fixtures" do
100119
run_generator
101-
expect(rails_helper).not_to have_transactional_fixtures_enabled?
120+
expect(rails_helper).not_to have_transactional_fixtures_enabled
102121
end
103122

104-
it "does not check use active record migration options" do
123+
specify "without schema maintenance checks" do
105124
run_generator
106125
expect(rails_helper).not_to use_active_record_migration
107126
expect(rails_helper).not_to maintain_test_schema
108127
end
109128
end
110-
111129
end

0 commit comments

Comments
 (0)