Skip to content

Commit

Permalink
Optional ActiveRecord config.
Browse files Browse the repository at this point in the history
Signed-off-by: Hermann Mayer <hermann.mayer92@gmail.com>
  • Loading branch information
Jack12816 committed Oct 25, 2018
1 parent 3f1e0f8 commit e9ca56f
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 23 deletions.
4 changes: 4 additions & 0 deletions lib/generators/rspec/install/templates/spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions lib/rspec/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 8 additions & 7 deletions lib/rspec/rails/fixture_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions spec/rspec/rails/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions spec/rspec/rails/fixture_support_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 15 additions & 11 deletions spec/rspec/rails/matchers/has_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
33 changes: 28 additions & 5 deletions spec/support/ar_classes.rb
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit e9ca56f

Please sign in to comment.