Skip to content

Commit 39b003b

Browse files
committed
Optional ActiveRecord config.
Signed-off-by: Hermann Mayer <[email protected]>
1 parent 3f1e0f8 commit 39b003b

File tree

7 files changed

+101
-23
lines changed

7 files changed

+101
-23
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
end
4343
<% end -%>
4444
RSpec.configure do |config|
45+
# You can explicitly turn off ActiveRecord support on rspec-rails by
46+
# uncommenting the following line.
47+
# config.use_active_record = false
48+
4549
<% if RSpec::Rails::FeatureCheck.has_active_record? -%>
4650
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
4751
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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,51 @@ 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+
it "raise due to no connection established" do
26+
example_group = RSpec::Core::ExampleGroup.describe("FixtureSupport") do
27+
include FixtureSupport
28+
include RSpec::Rails::MinitestLifecycleAdapter
29+
end
30+
31+
test = example_group.example("foo") do
32+
expect(true).to be(true)
33+
end
34+
35+
expect(example_group.run).to be(false)
36+
expect(test.execution_result.exception).to \
37+
be_a(ActiveRecord::ConnectionNotEstablished)
38+
end
39+
end
40+
41+
context "with use_active_record set to false" do
42+
before { RSpec.configuration.use_active_record = false }
43+
44+
after { RSpec.configuration.use_active_record = true }
45+
46+
it "does not raise" do
47+
example_group = RSpec::Core::ExampleGroup.describe("FixtureSupport") do
48+
include FixtureSupport
49+
include RSpec::Rails::MinitestLifecycleAdapter
50+
end
51+
52+
test = example_group.example("foo") do
53+
expect(true).to be(true)
54+
end
55+
56+
expect(example_group.run).to be(true)
57+
expect(test.execution_result.exception).not_to \
58+
be_a(ActiveRecord::ConnectionNotEstablished)
59+
end
60+
end
61+
end
1662
end
1763
end

spec/rspec/rails/matchers/has_spec.rb

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
require 'spec_helper'
22

33
class CollectionOwner < ActiveRecord::Base
4-
connection.execute <<-SQL
5-
CREATE TABLE collection_owners (
6-
id integer PRIMARY KEY AUTOINCREMENT
7-
)
8-
SQL
94
has_many :associated_items do
105
def has_some_quality?; true end
116
end
127
end
138

149
class AssociatedItem < ActiveRecord::Base
15-
connection.execute <<-SQL
16-
CREATE TABLE associated_items (
17-
id integer PRIMARY KEY AUTOINCREMENT,
18-
collection_owner_id integer
19-
)
20-
SQL
2110
belongs_to :collection_owner
2211
end
2312

2413
describe "should have_xxx" do
14+
before(:all) do
15+
CollectionOwner.connection.execute <<-SQL
16+
CREATE TABLE collection_owners (
17+
id integer PRIMARY KEY AUTOINCREMENT
18+
)
19+
SQL
20+
21+
AssociatedItem.connection.execute <<-SQL
22+
CREATE TABLE associated_items (
23+
id integer PRIMARY KEY AUTOINCREMENT,
24+
collection_owner_id integer
25+
)
26+
SQL
27+
end
28+
2529
it "works with ActiveRecord::Associations::CollectionProxy" do
2630
owner = CollectionOwner.new
2731
expect(owner.associated_items).to have_some_quality

spec/support/ar_classes.rb

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
1-
ActiveRecord::Base.establish_connection(
2-
:adapter => 'sqlite3',
3-
:database => ':memory:'
4-
)
1+
def establish_active_record_connection
2+
ActiveRecord::Base.establish_connection(
3+
:adapter => 'sqlite3',
4+
:database => ':memory:'
5+
)
6+
7+
[:MockableModel,
8+
:SubMockableModel,
9+
:AssociatedModel,
10+
:AlternatePrimaryKeyModel].each do |host|
11+
next unless Kernel.const_defined? host
12+
Connections.extended(Kernel.const_get(host))
13+
end
14+
end
15+
16+
def clear_active_record_connection
17+
ActiveRecord::Base.connection_handler.clear_all_connections!
18+
ActiveRecord::Base.connection_handler.connection_pool_list.each do |con|
19+
ActiveRecord::Base.connection_handler.remove_connection(con.spec.name)
20+
end
21+
end
22+
23+
establish_active_record_connection
524

625
module Connections
726
def self.extended(host)
827
host.connection.execute <<-eosql
9-
CREATE TABLE #{host.table_name} (
28+
CREATE TABLE IF NOT EXISTS #{host.table_name} (
1029
#{host.primary_key} integer PRIMARY KEY AUTOINCREMENT,
1130
associated_model_id integer,
1231
mockable_model_id integer,

0 commit comments

Comments
 (0)