Skip to content

Commit 85e23f3

Browse files
committed
Optional ActiveRecord config.
Signed-off-by: Hermann Mayer <[email protected]>
1 parent f227983 commit 85e23f3

File tree

7 files changed

+93
-11
lines changed

7 files changed

+93
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ bundle
2222
specs.out
2323
spec/examples.txt
2424
specs.out
25+
test.db

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
end
4747
<% end -%>
4848
RSpec.configure do |config|
49+
# You can explicitly turn off ActiveRecord support on rspec-rails by
50+
# uncommenting the following line.
51+
# config.use_active_record = false
52+
4953
<% if RSpec::Rails::FeatureCheck.has_active_record? -%>
5054
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
5155
config.fixture_path = "#{::Rails.root}/spec/fixtures"

lib/rspec/rails/configuration.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def self.initialize_configuration(config)
6262
config.add_setting :infer_base_class_for_anonymous_controllers, :default => true
6363

6464
# fixture support
65+
config.add_setting :use_active_record, :default => true
6566
config.add_setting :use_transactional_fixtures, :alias_with => :use_transactional_examples
6667
config.add_setting :use_instantiated_fixtures
6768
config.add_setting :global_fixtures

lib/rspec/rails/fixture_support.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ 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 if ::ActiveRecord::VERSION::STRING > '4'
9-
include RSpec::Rails::MinitestAssertionAdapter
10-
include ActiveRecord::TestFixtures
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 if ::ActiveRecord::VERSION::STRING > '4'
11+
include RSpec::Rails::MinitestAssertionAdapter
12+
include ActiveRecord::TestFixtures
1113

12-
included do
1314
# TODO: (DC 2011-06-25) this is necessary because fixture_file_upload
1415
# accesses fixture_path directly on ActiveSupport::TestCase. This is
1516
# fixed in rails by https://github.com/rails/rails/pull/1861, which

spec/rspec/rails/configuration_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
:use_transactional_fixtures,
6666
:alias_with => :use_transactional_examples
6767

68+
include_examples "adds setting", :use_active_record,
69+
:default => true
70+
6871
include_examples "adds setting", :use_instantiated_fixtures
6972

7073
include_examples "adds setting", :global_fixtures

spec/rspec/rails/fixture_support_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,54 @@ module RSpec::Rails
1313
expect(group).to respond_to(:fixture_path=)
1414
end
1515
end
16+
17+
context "without database available" do
18+
before { clear_active_record_connection }
19+
20+
after { establish_active_record_connection }
21+
22+
context "with use_active_record set to true" do
23+
before { RSpec.configuration.use_active_record = true }
24+
25+
# Not all Rails versions raise the
26+
# +ActiveRecord::ConnectionNotEstablished+ exception at this point.
27+
# (Eg. Rails 3.x, 4.0, 5.0)
28+
it "raise due to no connection established" do
29+
example_group = RSpec::Core::ExampleGroup.describe("FixtureSupport") do
30+
include FixtureSupport
31+
include RSpec::Rails::MinitestLifecycleAdapter
32+
end
33+
34+
test = example_group.example("foo") do
35+
expect(true).to be(true)
36+
end
37+
38+
success = example_group.run
39+
expect(test.execution_result.exception).to \
40+
be_a(ActiveRecord::ConnectionNotEstablished) unless success
41+
end
42+
end
43+
44+
context "with use_active_record set to false" do
45+
before { RSpec.configuration.use_active_record = false }
46+
47+
after { RSpec.configuration.use_active_record = true }
48+
49+
it "does not raise" do
50+
example_group = RSpec::Core::ExampleGroup.describe("FixtureSupport") do
51+
include FixtureSupport
52+
include RSpec::Rails::MinitestLifecycleAdapter
53+
end
54+
55+
test = example_group.example("foo") do
56+
expect(true).to be(true)
57+
end
58+
59+
expect(example_group.run).to be(true)
60+
expect(test.execution_result.exception).not_to \
61+
be_a(ActiveRecord::ConnectionNotEstablished)
62+
end
63+
end
64+
end
1665
end
1766
end

spec/support/ar_classes.rb

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
1-
ActiveRecord::Base.establish_connection(
2-
:adapter => 'sqlite3',
3-
:database => ':memory:'
4-
)
1+
FileUtils.rm_f('./test.db')
2+
3+
def establish_active_record_connection
4+
ActiveRecord::Base.establish_connection(
5+
:adapter => 'sqlite3',
6+
:database => './test.db'
7+
)
8+
end
9+
10+
def clear_active_record_connection
11+
ActiveRecord::Base.connection_handler.clear_all_connections!
12+
if ::Rails::VERSION::MAJOR >= 4
13+
ActiveRecord::Base.connection_handler.connection_pool_list.each do |pool|
14+
if ::Rails::VERSION::MAJOR >= 5
15+
ActiveRecord::Base.connection_handler.remove_connection(pool.spec.name)
16+
elsif ::Rails::VERSION::MAJOR >= 4
17+
ActiveRecord::Base.connection_handler.remove_connection(ActiveRecord::Base)
18+
end
19+
end
20+
else
21+
ActiveRecord::Base.connection_handler.connection_pools.each do |_name, _pool|
22+
ActiveRecord::Base.connection_handler.remove_connection(ActiveRecord::Base)
23+
end
24+
end
25+
end
26+
27+
establish_active_record_connection
528

629
module Connections
730
def self.extended(host)

0 commit comments

Comments
 (0)