From e9ca56f62981130e43005b001fa2669c824dbb30 Mon Sep 17 00:00:00 2001 From: Hermann Mayer Date: Wed, 25 Jul 2018 19:44:52 +0200 Subject: [PATCH] Optional ActiveRecord config. Signed-off-by: Hermann Mayer --- .../install/templates/spec/rails_helper.rb | 4 ++ lib/rspec/rails/configuration.rb | 1 + lib/rspec/rails/fixture_support.rb | 15 +++--- spec/rspec/rails/configuration_spec.rb | 3 ++ spec/rspec/rails/fixture_support_spec.rb | 46 +++++++++++++++++++ spec/rspec/rails/matchers/has_spec.rb | 26 ++++++----- spec/support/ar_classes.rb | 33 +++++++++++-- 7 files changed, 105 insertions(+), 23 deletions(-) diff --git a/lib/generators/rspec/install/templates/spec/rails_helper.rb b/lib/generators/rspec/install/templates/spec/rails_helper.rb index bc0bc7d4ab..8cd273dc9f 100644 --- a/lib/generators/rspec/install/templates/spec/rails_helper.rb +++ b/lib/generators/rspec/install/templates/spec/rails_helper.rb @@ -42,6 +42,10 @@ end <% end -%> RSpec.configure do |config| + # You can explicitly turn off ActiveRecord support on rspec-rails by + # uncommenting the following line. + # config.use_active_record = false + <% if RSpec::Rails::FeatureCheck.has_active_record? -%> # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures config.fixture_path = "#{::Rails.root}/spec/fixtures" diff --git a/lib/rspec/rails/configuration.rb b/lib/rspec/rails/configuration.rb index 6869b88437..64eca7e788 100644 --- a/lib/rspec/rails/configuration.rb +++ b/lib/rspec/rails/configuration.rb @@ -62,6 +62,7 @@ def self.initialize_configuration(config) config.add_setting :infer_base_class_for_anonymous_controllers, :default => true # fixture support + config.add_setting :use_active_record, :default => true config.add_setting :use_transactional_fixtures, :alias_with => :use_transactional_examples config.add_setting :use_instantiated_fixtures config.add_setting :global_fixtures diff --git a/lib/rspec/rails/fixture_support.rb b/lib/rspec/rails/fixture_support.rb index 1855588261..1d6077a7eb 100644 --- a/lib/rspec/rails/fixture_support.rb +++ b/lib/rspec/rails/fixture_support.rb @@ -2,14 +2,15 @@ module RSpec module Rails # @private module FixtureSupport - if defined?(ActiveRecord::TestFixtures) - extend ActiveSupport::Concern - include RSpec::Rails::SetupAndTeardownAdapter - include RSpec::Rails::MinitestLifecycleAdapter if ::ActiveRecord::VERSION::STRING > '4' - include RSpec::Rails::MinitestAssertionAdapter - include ActiveRecord::TestFixtures + extend ActiveSupport::Concern + + included do + if RSpec.configuration.use_active_record? && defined?(ActiveRecord::TestFixtures) + include RSpec::Rails::SetupAndTeardownAdapter + include RSpec::Rails::MinitestLifecycleAdapter if ::ActiveRecord::VERSION::STRING > '4' + include RSpec::Rails::MinitestAssertionAdapter + include ActiveRecord::TestFixtures - included do # TODO: (DC 2011-06-25) this is necessary because fixture_file_upload # accesses fixture_path directly on ActiveSupport::TestCase. This is # fixed in rails by https://github.com/rails/rails/pull/1861, which diff --git a/spec/rspec/rails/configuration_spec.rb b/spec/rspec/rails/configuration_spec.rb index 5538eeffa8..c21969fb10 100644 --- a/spec/rspec/rails/configuration_spec.rb +++ b/spec/rspec/rails/configuration_spec.rb @@ -65,6 +65,9 @@ :use_transactional_fixtures, :alias_with => :use_transactional_examples + include_examples "adds setting", :use_active_record, + :default => true + include_examples "adds setting", :use_instantiated_fixtures include_examples "adds setting", :global_fixtures diff --git a/spec/rspec/rails/fixture_support_spec.rb b/spec/rspec/rails/fixture_support_spec.rb index 29243db089..890824bd6c 100644 --- a/spec/rspec/rails/fixture_support_spec.rb +++ b/spec/rspec/rails/fixture_support_spec.rb @@ -13,5 +13,51 @@ module RSpec::Rails expect(group).to respond_to(:fixture_path=) end end + + context "without database available" do + before { clear_active_record_connection } + + after { establish_active_record_connection } + + context "with use_active_record set to true" do + before { RSpec.configuration.use_active_record = true } + + it "raise due to no connection established" do + example_group = RSpec::Core::ExampleGroup.describe("FixtureSupport") do + include FixtureSupport + include RSpec::Rails::MinitestLifecycleAdapter + end + + test = example_group.example("foo") do + expect(true).to be(true) + end + + expect(example_group.run).to be(false) + expect(test.execution_result.exception).to \ + be_a(ActiveRecord::ConnectionNotEstablished) + end + end + + context "with use_active_record set to false" do + before { RSpec.configuration.use_active_record = false } + + after { RSpec.configuration.use_active_record = true } + + it "does not raise" do + example_group = RSpec::Core::ExampleGroup.describe("FixtureSupport") do + include FixtureSupport + include RSpec::Rails::MinitestLifecycleAdapter + end + + test = example_group.example("foo") do + expect(true).to be(true) + end + + expect(example_group.run).to be(true) + expect(test.execution_result.exception).not_to \ + be_a(ActiveRecord::ConnectionNotEstablished) + end + end + end end end diff --git a/spec/rspec/rails/matchers/has_spec.rb b/spec/rspec/rails/matchers/has_spec.rb index c634777752..06d240c77f 100644 --- a/spec/rspec/rails/matchers/has_spec.rb +++ b/spec/rspec/rails/matchers/has_spec.rb @@ -1,27 +1,31 @@ require 'spec_helper' class CollectionOwner < ActiveRecord::Base - connection.execute <<-SQL - CREATE TABLE collection_owners ( - id integer PRIMARY KEY AUTOINCREMENT - ) -SQL has_many :associated_items do def has_some_quality?; true end end end class AssociatedItem < ActiveRecord::Base - connection.execute <<-SQL - CREATE TABLE associated_items ( - id integer PRIMARY KEY AUTOINCREMENT, - collection_owner_id integer - ) -SQL belongs_to :collection_owner end describe "should have_xxx" do + before(:all) do + CollectionOwner.connection.execute <<-SQL + CREATE TABLE collection_owners ( + id integer PRIMARY KEY AUTOINCREMENT + ) + SQL + + AssociatedItem.connection.execute <<-SQL + CREATE TABLE associated_items ( + id integer PRIMARY KEY AUTOINCREMENT, + collection_owner_id integer + ) + SQL + end + it "works with ActiveRecord::Associations::CollectionProxy" do owner = CollectionOwner.new expect(owner.associated_items).to have_some_quality diff --git a/spec/support/ar_classes.rb b/spec/support/ar_classes.rb index e7b85cb6de..4434dc1b64 100644 --- a/spec/support/ar_classes.rb +++ b/spec/support/ar_classes.rb @@ -1,12 +1,35 @@ -ActiveRecord::Base.establish_connection( - :adapter => 'sqlite3', - :database => ':memory:' -) +def establish_active_record_connection + ActiveRecord::Base.establish_connection( + :adapter => 'sqlite3', + :database => ':memory:' + ) + + [:MockableModel, + :SubMockableModel, + :AssociatedModel, + :AlternatePrimaryKeyModel].each do |host| + next unless Kernel.const_defined? host + Connections.extended(Kernel.const_get(host)) + end +end + +def clear_active_record_connection + ActiveRecord::Base.connection_handler.clear_all_connections! + ActiveRecord::Base.connection_handler.connection_pool_list.each do |con| + if ::Rails::VERSION::STRING > '5' + ActiveRecord::Base.connection_handler.remove_connection(con.spec.name) + elsif ::Rails::VERSION::STRING > '3' + ActiveRecord::Base.connection_handler.remove_connection(ActiveRecord::Base) + end + end +end + +establish_active_record_connection module Connections def self.extended(host) host.connection.execute <<-eosql - CREATE TABLE #{host.table_name} ( + CREATE TABLE IF NOT EXISTS #{host.table_name} ( #{host.primary_key} integer PRIMARY KEY AUTOINCREMENT, associated_model_id integer, mockable_model_id integer,