fix more global state spec leakage.

This commit is contained in:
Micah Geisel 2018-04-26 12:06:01 -07:00
parent 0fbddcc03b
commit 0c44b931a2
5 changed files with 197 additions and 236 deletions

View file

@ -9,33 +9,35 @@ class FakeModel
end
end
module DatabaseCleaner
describe ActiveRecord do
it { is_expected.to respond_to(:available_strategies) }
describe DatabaseCleaner::ActiveRecord do
it { is_expected.to respond_to(:available_strategies) }
describe "config_file_location" do
subject { ActiveRecord.config_file_location }
it "should default to DatabaseCleaner.root / config / database.yml" do
ActiveRecord.config_file_location=nil
expect(DatabaseCleaner).to receive(:app_root).and_return("/path/to")
expect(subject).to eq '/path/to/config/database.yml'
end
describe "config_file_location" do
after do
# prevent global state leakage
DatabaseCleaner::ActiveRecord.config_file_location=nil
end
it "should default to DatabaseCleaner.root / config / database.yml" do
DatabaseCleaner::ActiveRecord.config_file_location=nil
expect(DatabaseCleaner).to receive(:app_root).and_return("/path/to")
expect(DatabaseCleaner::ActiveRecord.config_file_location).to eq '/path/to/config/database.yml'
end
end
end
module DatabaseCleaner
module ActiveRecord
class ExampleStrategy
include ::DatabaseCleaner::ActiveRecord::Base
include DatabaseCleaner::ActiveRecord::Base
end
describe ExampleStrategy do
let :config_location do
'/path/to/config/database.yml'
end
let(:config_location) { '/path/to/config/database.yml' }
before { allow(::DatabaseCleaner::ActiveRecord).to receive(:config_file_location).and_return(config_location) }
before do
allow(DatabaseCleaner::ActiveRecord).to receive(:config_file_location).and_return(config_location)
end
it_should_behave_like "a generic strategy"

View file

@ -54,7 +54,7 @@ module ActiveRecord
it "should not truncate the tables specified in the :except option" do
2.times { User.create }
::DatabaseCleaner::ActiveRecord::Truncation.new(:except => ['users']).clean
DatabaseCleaner::ActiveRecord::Truncation.new(:except => ['users']).clean
expect( User.count ).to eq 2
end
end

View file

@ -15,60 +15,17 @@ module DatabaseCleaner
}
describe "autodetect" do
#Cache all ORMs, we'll need them later but not now.
before(:all) do
Temp_AR = ::ActiveRecord if defined?(::ActiveRecord) and not defined?(Temp_AR)
Temp_DM = ::DataMapper if defined?(::DataMapper) and not defined?(Temp_DM)
Temp_MM = ::MongoMapper if defined?(::MongoMapper) and not defined?(Temp_MM)
Temp_MO = ::Mongoid if defined?(::Mongoid) and not defined?(Temp_MO)
Temp_CP = ::CouchPotato if defined?(::CouchPotato) and not defined?(Temp_CP)
Temp_SQ = ::Sequel if defined?(::Sequel) and not defined?(Temp_SQ)
Temp_MP = ::Moped if defined?(::Moped) and not defined?(Temp_MP)
Temp_RS = ::Redis if defined?(::Redis) and not defined?(Temp_RS)
Temp_OH = ::Ohm if defined?(::Ohm) and not defined?(Temp_OH)
Temp_NJ = ::Neo4j if defined?(::Neo4j) and not defined?(Temp_NJ)
end
#Remove all ORM mocks and restore from cache
after(:all) do
Object.send(:remove_const, 'ActiveRecord') if defined?(::ActiveRecord)
Object.send(:remove_const, 'DataMapper') if defined?(::DataMapper)
Object.send(:remove_const, 'MongoMapper') if defined?(::MongoMapper)
Object.send(:remove_const, 'Mongoid') if defined?(::Mongoid)
Object.send(:remove_const, 'CouchPotato') if defined?(::CouchPotato)
Object.send(:remove_const, 'Sequel') if defined?(::Sequel)
Object.send(:remove_const, 'Moped') if defined?(::Moped)
Object.send(:remove_const, 'Ohm') if defined?(::Ohm)
Object.send(:remove_const, 'Redis') if defined?(::Redis)
Object.send(:remove_const, 'Neo4j') if defined?(::Neo4j)
# Restore ORMs
::ActiveRecord = Temp_AR if defined? Temp_AR
::DataMapper = Temp_DM if defined? Temp_DM
::MongoMapper = Temp_MM if defined? Temp_MM
::Mongoid = Temp_MO if defined? Temp_MO
::CouchPotato = Temp_CP if defined? Temp_CP
::Sequel = Temp_SQ if defined? Temp_SQ
::Moped = Temp_MP if defined? Temp_MP
::Ohm = Temp_OH if defined? Temp_OH
::Redis = Temp_RS if defined? Temp_RS
::Neo4j = Temp_NJ if defined? Temp_NJ
end
#reset the orm mocks
before(:each) do
Object.send(:remove_const, 'ActiveRecord') if defined?(::ActiveRecord)
Object.send(:remove_const, 'DataMapper') if defined?(::DataMapper)
Object.send(:remove_const, 'MongoMapper') if defined?(::MongoMapper)
Object.send(:remove_const, 'Mongoid') if defined?(::Mongoid)
Object.send(:remove_const, 'CouchPotato') if defined?(::CouchPotato)
Object.send(:remove_const, 'Sequel') if defined?(::Sequel)
Object.send(:remove_const, 'Moped') if defined?(::Moped)
Object.send(:remove_const, 'Ohm') if defined?(::Ohm)
Object.send(:remove_const, 'Redis') if defined?(::Redis)
Object.send(:remove_const, 'Neo4j') if defined?(::Neo4j)
before do
hide_const("ActiveRecord")
hide_const("DataMapper")
hide_const("MongoMapper")
hide_const("Mongoid")
hide_const("CouchPotato")
hide_const("Sequel")
hide_const("Moped")
hide_const("Redis")
hide_const("Ohm")
hide_const("Neo4j")
end
let(:cleaner) { DatabaseCleaner::Base.new :autodetect }
@ -78,112 +35,112 @@ module DatabaseCleaner
end
it "should detect ActiveRecord first" do
Object.const_set('ActiveRecord','Actively mocking records.')
Object.const_set('DataMapper', 'Mapping data mocks')
Object.const_set('MongoMapper', 'Mapping mock mongos')
Object.const_set('Mongoid', 'Mongoid mock')
Object.const_set('CouchPotato', 'Couching mock potatos')
Object.const_set('Sequel', 'Sequel mock')
Object.const_set('Moped', 'Moped mock')
Object.const_set('Ohm', 'Ohm mock')
Object.const_set('Redis', 'Redis mock')
Object.const_set('Neo4j', 'Neo4j mock')
stub_const('ActiveRecord','Actively mocking records.')
stub_const('DataMapper', 'Mapping data mocks')
stub_const('MongoMapper', 'Mapping mock mongos')
stub_const('Mongoid', 'Mongoid mock')
stub_const('CouchPotato', 'Couching mock potatos')
stub_const('Sequel', 'Sequel mock')
stub_const('Moped', 'Moped mock')
stub_const('Ohm', 'Ohm mock')
stub_const('Redis', 'Redis mock')
stub_const('Neo4j', 'Neo4j mock')
expect(cleaner.orm).to eq :active_record
expect(cleaner).to be_auto_detected
end
it "should detect DataMapper second" do
Object.const_set('DataMapper', 'Mapping data mocks')
Object.const_set('MongoMapper', 'Mapping mock mongos')
Object.const_set('Mongoid', 'Mongoid mock')
Object.const_set('CouchPotato', 'Couching mock potatos')
Object.const_set('Sequel', 'Sequel mock')
Object.const_set('Moped', 'Moped mock')
Object.const_set('Ohm', 'Ohm mock')
Object.const_set('Redis', 'Redis mock')
Object.const_set('Neo4j', 'Neo4j mock')
stub_const('DataMapper', 'Mapping data mocks')
stub_const('MongoMapper', 'Mapping mock mongos')
stub_const('Mongoid', 'Mongoid mock')
stub_const('CouchPotato', 'Couching mock potatos')
stub_const('Sequel', 'Sequel mock')
stub_const('Moped', 'Moped mock')
stub_const('Ohm', 'Ohm mock')
stub_const('Redis', 'Redis mock')
stub_const('Neo4j', 'Neo4j mock')
expect(cleaner.orm).to eq :data_mapper
expect(cleaner).to be_auto_detected
end
it "should detect MongoMapper third" do
Object.const_set('MongoMapper', 'Mapping mock mongos')
Object.const_set('Mongoid', 'Mongoid mock')
Object.const_set('CouchPotato', 'Couching mock potatos')
Object.const_set('Sequel', 'Sequel mock')
Object.const_set('Moped', 'Moped mock')
Object.const_set('Ohm', 'Ohm mock')
Object.const_set('Redis', 'Redis mock')
Object.const_set('Neo4j', 'Neo4j mock')
stub_const('MongoMapper', 'Mapping mock mongos')
stub_const('Mongoid', 'Mongoid mock')
stub_const('CouchPotato', 'Couching mock potatos')
stub_const('Sequel', 'Sequel mock')
stub_const('Moped', 'Moped mock')
stub_const('Ohm', 'Ohm mock')
stub_const('Redis', 'Redis mock')
stub_const('Neo4j', 'Neo4j mock')
expect(cleaner.orm).to eq :mongo_mapper
expect(cleaner).to be_auto_detected
end
it "should detect Mongoid fourth" do
Object.const_set('Mongoid', 'Mongoid mock')
Object.const_set('CouchPotato', 'Couching mock potatos')
Object.const_set('Sequel', 'Sequel mock')
Object.const_set('Moped', 'Moped mock')
Object.const_set('Ohm', 'Ohm mock')
Object.const_set('Redis', 'Redis mock')
Object.const_set('Neo4j', 'Neo4j mock')
stub_const('Mongoid', 'Mongoid mock')
stub_const('CouchPotato', 'Couching mock potatos')
stub_const('Sequel', 'Sequel mock')
stub_const('Moped', 'Moped mock')
stub_const('Ohm', 'Ohm mock')
stub_const('Redis', 'Redis mock')
stub_const('Neo4j', 'Neo4j mock')
expect(cleaner.orm).to eq :mongoid
expect(cleaner).to be_auto_detected
end
it "should detect CouchPotato fifth" do
Object.const_set('CouchPotato', 'Couching mock potatos')
Object.const_set('Sequel', 'Sequel mock')
Object.const_set('Moped', 'Moped mock')
Object.const_set('Ohm', 'Ohm mock')
Object.const_set('Redis', 'Redis mock')
Object.const_set('Neo4j', 'Neo4j mock')
stub_const('CouchPotato', 'Couching mock potatos')
stub_const('Sequel', 'Sequel mock')
stub_const('Moped', 'Moped mock')
stub_const('Ohm', 'Ohm mock')
stub_const('Redis', 'Redis mock')
stub_const('Neo4j', 'Neo4j mock')
expect(cleaner.orm).to eq :couch_potato
expect(cleaner).to be_auto_detected
end
it "should detect Sequel sixth" do
Object.const_set('Sequel', 'Sequel mock')
Object.const_set('Moped', 'Moped mock')
Object.const_set('Ohm', 'Ohm mock')
Object.const_set('Redis', 'Redis mock')
Object.const_set('Neo4j', 'Neo4j mock')
stub_const('Sequel', 'Sequel mock')
stub_const('Moped', 'Moped mock')
stub_const('Ohm', 'Ohm mock')
stub_const('Redis', 'Redis mock')
stub_const('Neo4j', 'Neo4j mock')
expect(cleaner.orm).to eq :sequel
expect(cleaner).to be_auto_detected
end
it 'detects Moped seventh' do
Object.const_set('Moped', 'Moped mock')
stub_const('Moped', 'Moped mock')
expect(cleaner.orm).to eq :moped
expect(cleaner).to be_auto_detected
end
it 'detects Ohm eighth' do
Object.const_set('Ohm', 'Ohm mock')
Object.const_set('Redis', 'Redis mock')
Object.const_set('Neo4j', 'Neo4j mock')
stub_const('Ohm', 'Ohm mock')
stub_const('Redis', 'Redis mock')
stub_const('Neo4j', 'Neo4j mock')
expect(cleaner.orm).to eq :ohm
expect(cleaner).to be_auto_detected
end
it 'detects Redis ninth' do
Object.const_set('Redis', 'Redis mock')
Object.const_set('Neo4j', 'Neo4j mock')
stub_const('Redis', 'Redis mock')
stub_const('Neo4j', 'Neo4j mock')
expect(cleaner.orm).to eq :redis
expect(cleaner).to be_auto_detected
end
it 'detects Neo4j tenth' do
Object.const_set('Neo4j', 'Neo4j mock')
stub_const('Neo4j', 'Neo4j mock')
expect(cleaner.orm).to eq :neo4j
expect(cleaner).to be_auto_detected

View file

@ -10,6 +10,7 @@ module DatabaseCleaner
def reset
@cleaners = nil
@connections = nil
@app_root = nil
end
# hackey, hack.. connections needs to stick around until I can properly deprecate the API
def connections_stub(array)
@ -19,192 +20,192 @@ module DatabaseCleaner
end
end
describe ::DatabaseCleaner do
before(:each) { ::DatabaseCleaner.reset }
describe DatabaseCleaner do
before(:each) { DatabaseCleaner.reset }
context "orm specification" do
it "should not accept unrecognised orms" do
expect { ::DatabaseCleaner[nil] }.to raise_error(::DatabaseCleaner::NoORMDetected)
expect { DatabaseCleaner[nil] }.to raise_error(DatabaseCleaner::NoORMDetected)
end
it "should accept :active_record" do
cleaner = ::DatabaseCleaner[:active_record]
expect(cleaner).to be_a(::DatabaseCleaner::Base)
cleaner = DatabaseCleaner[:active_record]
expect(cleaner).to be_a(DatabaseCleaner::Base)
expect(cleaner.orm).to eq :active_record
expect(::DatabaseCleaner.connections.size).to eq 1
expect(DatabaseCleaner.connections.size).to eq 1
end
it "should accept :data_mapper" do
cleaner = ::DatabaseCleaner[:data_mapper]
expect(cleaner).to be_a(::DatabaseCleaner::Base)
cleaner = DatabaseCleaner[:data_mapper]
expect(cleaner).to be_a(DatabaseCleaner::Base)
expect(cleaner.orm).to eq :data_mapper
expect(::DatabaseCleaner.connections.size).to eq 1
expect(DatabaseCleaner.connections.size).to eq 1
end
it "should accept :mongo_mapper" do
cleaner = ::DatabaseCleaner[:mongo_mapper]
expect(cleaner).to be_a(::DatabaseCleaner::Base)
cleaner = DatabaseCleaner[:mongo_mapper]
expect(cleaner).to be_a(DatabaseCleaner::Base)
expect(cleaner.orm).to eq :mongo_mapper
expect(::DatabaseCleaner.connections.size).to eq 1
expect(DatabaseCleaner.connections.size).to eq 1
end
it "should accept :couch_potato" do
cleaner = ::DatabaseCleaner[:couch_potato]
expect(cleaner).to be_a(::DatabaseCleaner::Base)
cleaner = DatabaseCleaner[:couch_potato]
expect(cleaner).to be_a(DatabaseCleaner::Base)
expect(cleaner.orm).to eq :couch_potato
expect(::DatabaseCleaner.connections.size).to eq 1
expect(DatabaseCleaner.connections.size).to eq 1
end
it "should accept :moped" do
cleaner = ::DatabaseCleaner[:moped]
expect(cleaner).to be_a(::DatabaseCleaner::Base)
cleaner = DatabaseCleaner[:moped]
expect(cleaner).to be_a(DatabaseCleaner::Base)
expect(cleaner.orm).to eq :moped
expect(::DatabaseCleaner.connections.size).to eq 1
expect(DatabaseCleaner.connections.size).to eq 1
end
it 'accepts :ohm' do
cleaner = ::DatabaseCleaner[:ohm]
expect(cleaner).to be_a(::DatabaseCleaner::Base)
cleaner = DatabaseCleaner[:ohm]
expect(cleaner).to be_a(DatabaseCleaner::Base)
expect(cleaner.orm).to eq :ohm
expect(::DatabaseCleaner.connections.size).to eq 1
expect(DatabaseCleaner.connections.size).to eq 1
end
end
it "should accept multiple orm's" do
::DatabaseCleaner[:couch_potato]
::DatabaseCleaner[:data_mapper]
expect(::DatabaseCleaner.connections.size).to eq 2
expect(::DatabaseCleaner.connections[0].orm).to eq :couch_potato
expect(::DatabaseCleaner.connections[1].orm).to eq :data_mapper
DatabaseCleaner[:couch_potato]
DatabaseCleaner[:data_mapper]
expect(DatabaseCleaner.connections.size).to eq 2
expect(DatabaseCleaner.connections[0].orm).to eq :couch_potato
expect(DatabaseCleaner.connections[1].orm).to eq :data_mapper
end
context "connection/db specification" do
it "should accept a connection parameter and store it" do
cleaner = ::DatabaseCleaner[:active_record, {:connection => :first_connection}]
expect(cleaner).to be_a(::DatabaseCleaner::Base)
cleaner = DatabaseCleaner[:active_record, {:connection => :first_connection}]
expect(cleaner).to be_a(DatabaseCleaner::Base)
expect(cleaner.orm).to eq :active_record
expect(cleaner.db).to eq :first_connection
end
it "should accept multiple connections for a single orm" do
::DatabaseCleaner[:data_mapper,{:connection => :first_db}]
::DatabaseCleaner[:data_mapper,{:connection => :second_db}]
expect(::DatabaseCleaner.connections.size).to eq 2
expect(::DatabaseCleaner.connections[0].orm).to eq :data_mapper
expect(::DatabaseCleaner.connections[0].db).to eq :first_db
expect(::DatabaseCleaner.connections[1].orm).to eq :data_mapper
expect(::DatabaseCleaner.connections[1].db).to eq :second_db
DatabaseCleaner[:data_mapper,{:connection => :first_db}]
DatabaseCleaner[:data_mapper,{:connection => :second_db}]
expect(DatabaseCleaner.connections.size).to eq 2
expect(DatabaseCleaner.connections[0].orm).to eq :data_mapper
expect(DatabaseCleaner.connections[0].db).to eq :first_db
expect(DatabaseCleaner.connections[1].orm).to eq :data_mapper
expect(DatabaseCleaner.connections[1].db).to eq :second_db
end
it "should accept multiple connections and multiple orms" do
::DatabaseCleaner[:data_mapper, {:connection => :first_db} ]
::DatabaseCleaner[:active_record,{:connection => :second_db}]
::DatabaseCleaner[:active_record,{:connection => :first_db} ]
::DatabaseCleaner[:data_mapper, {:connection => :second_db}]
DatabaseCleaner[:data_mapper, {:connection => :first_db} ]
DatabaseCleaner[:active_record,{:connection => :second_db}]
DatabaseCleaner[:active_record,{:connection => :first_db} ]
DatabaseCleaner[:data_mapper, {:connection => :second_db}]
expect(::DatabaseCleaner.connections.size).to eq 4
expect(DatabaseCleaner.connections.size).to eq 4
expect(::DatabaseCleaner.connections[0].orm).to eq :data_mapper
expect(::DatabaseCleaner.connections[0].db).to eq :first_db
expect(DatabaseCleaner.connections[0].orm).to eq :data_mapper
expect(DatabaseCleaner.connections[0].db).to eq :first_db
expect(::DatabaseCleaner.connections[1].orm).to eq :active_record
expect(::DatabaseCleaner.connections[1].db).to eq :second_db
expect(DatabaseCleaner.connections[1].orm).to eq :active_record
expect(DatabaseCleaner.connections[1].db).to eq :second_db
expect(::DatabaseCleaner.connections[2].orm).to eq :active_record
expect(::DatabaseCleaner.connections[2].db).to eq :first_db
expect(DatabaseCleaner.connections[2].orm).to eq :active_record
expect(DatabaseCleaner.connections[2].db).to eq :first_db
expect(::DatabaseCleaner.connections[3].orm).to eq :data_mapper
expect(::DatabaseCleaner.connections[3].db).to eq :second_db
expect(DatabaseCleaner.connections[3].orm).to eq :data_mapper
expect(DatabaseCleaner.connections[3].db).to eq :second_db
end
end
context "connection/db retrieval" do
it "should retrieve a db rather than create a new one" do
pending
connection = ::DatabaseCleaner[:active_record].strategy = :truncation
expect(::DatabaseCleaner[:active_record]).to eq connection
connection = DatabaseCleaner[:active_record].strategy = :truncation
expect(DatabaseCleaner[:active_record]).to eq connection
end
end
context "class methods" do
subject { ::DatabaseCleaner }
subject { DatabaseCleaner }
it "should give me a default (autodetection) databasecleaner by default" do
cleaner = double("cleaner").as_null_object
allow(::DatabaseCleaner::Base).to receive(:new).and_return(cleaner)
allow(DatabaseCleaner::Base).to receive(:new).and_return(cleaner)
expect(::DatabaseCleaner.connections).to eq [cleaner]
expect(DatabaseCleaner.connections).to eq [cleaner]
end
end
context "single orm single connection" do
let(:connection) { ::DatabaseCleaner.connections.first }
let(:connection) { DatabaseCleaner.connections.first }
it "should proxy strategy=" do
stratagum = double("stratagum")
expect(connection).to receive(:strategy=).with(stratagum)
::DatabaseCleaner.strategy = stratagum
stratagem = double("stratagem")
expect(connection).to receive(:strategy=).with(stratagem)
DatabaseCleaner.strategy = stratagem
end
it "should proxy orm=" do
orm = double("orm")
expect(connection).to receive(:orm=).with(orm)
::DatabaseCleaner.orm = orm
DatabaseCleaner.orm = orm
end
it "should proxy start" do
expect(connection).to receive(:start)
::DatabaseCleaner.start
DatabaseCleaner.start
end
it "should proxy clean" do
expect(connection).to receive(:clean)
::DatabaseCleaner.clean
DatabaseCleaner.clean
end
it 'should proxy cleaning' do
expect(connection).to receive(:cleaning)
::DatabaseCleaner.cleaning { }
DatabaseCleaner.cleaning { }
end
it "should proxy clean_with" do
stratagem = double("stratgem")
expect(connection).to receive(:clean_with).with(stratagem, {})
::DatabaseCleaner.clean_with stratagem, {}
DatabaseCleaner.clean_with stratagem, {}
end
end
context "multiple connections" do
#these are relativly simple, all we need to do is make sure all connections are cleaned/started/cleaned_with appropriatly.
# these are relatively simple, all we need to do is make sure all connections are cleaned/started/cleaned_with appropriately.
context "simple proxy methods" do
let(:active_record) { double("active_mock") }
let(:data_mapper) { double("data_mock") }
before(:each) do
allow(::DatabaseCleaner).to receive(:connections).and_return([active_record,data_mapper])
allow(DatabaseCleaner).to receive(:connections).and_return([active_record,data_mapper])
end
it "should proxy orm to all connections" do
expect(active_record).to receive(:orm=)
expect(data_mapper).to receive(:orm=)
::DatabaseCleaner.orm = double("orm")
DatabaseCleaner.orm = double("orm")
end
it "should proxy start to all connections" do
expect(active_record).to receive(:start)
expect(data_mapper).to receive(:start)
::DatabaseCleaner.start
DatabaseCleaner.start
end
it "should proxy clean to all connections" do
expect(active_record).to receive(:clean)
expect(data_mapper).to receive(:clean)
::DatabaseCleaner.clean
DatabaseCleaner.clean
end
it "should initiate cleaning on each connection, yield, and finish cleaning each connection" do
@ -218,7 +219,7 @@ describe ::DatabaseCleaner do
end
end
::DatabaseCleaner.cleaning do
DatabaseCleaner.cleaning do
expect(active_record.started).to eq(true)
expect(data_mapper.started).to eq(true)
expect(active_record.cleaned).to eq(nil)
@ -234,7 +235,7 @@ describe ::DatabaseCleaner do
expect(active_record).to receive(:clean_with).with(stratagem)
expect(data_mapper).to receive(:clean_with).with(stratagem)
::DatabaseCleaner.clean_with stratagem
DatabaseCleaner.clean_with stratagem
end
end
@ -247,7 +248,7 @@ describe ::DatabaseCleaner do
active_record_2 = double("active_mock_on_db_two").as_null_object
data_mapper_1 = double("data_mock_on_db_one").as_null_object
::DatabaseCleaner.connections_stub [active_record_1,active_record_2,data_mapper_1]
DatabaseCleaner.connections_stub [active_record_1,active_record_2,data_mapper_1]
expect(active_record_1).to receive(:orm=).with(:data_mapper)
expect(active_record_2).to receive(:orm=).with(:data_mapper)
@ -255,9 +256,9 @@ describe ::DatabaseCleaner do
expect(active_record_1).to receive(:==).with(data_mapper_1).and_return(true)
expect(::DatabaseCleaner.connections.size).to eq 3
::DatabaseCleaner.orm = :data_mapper
expect(::DatabaseCleaner.connections.size).to eq 2
expect(DatabaseCleaner.connections.size).to eq 3
DatabaseCleaner.orm = :data_mapper
expect(DatabaseCleaner.connections.size).to eq 2
end
it "should proxy strategy to all connections and remove duplicate connections" do
@ -265,16 +266,16 @@ describe ::DatabaseCleaner do
active_record_2 = double("active_mock_strategy_two").as_null_object
strategy = double("strategy")
::DatabaseCleaner.connections_stub [active_record_1,active_record_2]
DatabaseCleaner.connections_stub [active_record_1,active_record_2]
expect(active_record_1).to receive(:strategy=).with(strategy)
expect(active_record_2).to receive(:strategy=).with(strategy)
expect(active_record_1).to receive(:==).with(active_record_2).and_return(true)
expect(::DatabaseCleaner.connections.size).to eq 2
::DatabaseCleaner.strategy = strategy
expect(::DatabaseCleaner.connections.size).to eq 1
expect(DatabaseCleaner.connections.size).to eq 2
DatabaseCleaner.strategy = strategy
expect(DatabaseCleaner.connections.size).to eq 1
end
end
end
@ -284,10 +285,10 @@ describe ::DatabaseCleaner do
orm = double("orm")
connection = double("a datamapper connection", :orm => orm )
::DatabaseCleaner.connections_stub [connection,connection,connection]
DatabaseCleaner.connections_stub [connection,connection,connection]
::DatabaseCleaner.remove_duplicates
expect(::DatabaseCleaner.connections.size).to eq 1
DatabaseCleaner.remove_duplicates
expect(DatabaseCleaner.connections.size).to eq 1
end
end
@ -303,42 +304,43 @@ describe ::DatabaseCleaner do
end
describe "orm_module" do
subject { ::DatabaseCleaner }
subject { DatabaseCleaner }
let(:mod) { double }
it "should return DatabaseCleaner::ActiveRecord for :active_record" do
::DatabaseCleaner::ActiveRecord = double("ar module") unless defined? ::DatabaseCleaner::ActiveRecord
expect(subject.orm_module(:active_record)).to eq DatabaseCleaner::ActiveRecord
stub_const "DatabaseCleaner::ActiveRecord", mod
expect(subject.orm_module(:active_record)).to eq mod
end
it "should return DatabaseCleaner::DataMapper for :data_mapper" do
::DatabaseCleaner::DataMapper = double("dm module") unless defined? ::DatabaseCleaner::DataMapper
expect(subject.orm_module(:data_mapper)).to eq DatabaseCleaner::DataMapper
stub_const "DatabaseCleaner::DataMapper", mod
expect(subject.orm_module(:data_mapper)).to eq mod
end
it "should return DatabaseCleaner::MongoMapper for :mongo_mapper" do
::DatabaseCleaner::MongoMapper = double("mm module") unless defined? ::DatabaseCleaner::MongoMapper
expect(subject.orm_module(:mongo_mapper)).to eq DatabaseCleaner::MongoMapper
stub_const "DatabaseCleaner::MongoMapper", mod
expect(subject.orm_module(:mongo_mapper)).to eq mod
end
it "should return DatabaseCleaner::Mongoid for :mongoid" do
::DatabaseCleaner::Mongoid = double("mongoid module") unless defined? ::DatabaseCleaner::Mongoid
expect(subject.orm_module(:mongoid)).to eq DatabaseCleaner::Mongoid
stub_const "DatabaseCleaner::Mongoid", mod
expect(subject.orm_module(:mongoid)).to eq mod
end
it "should return DatabaseCleaner::Mongo for :mongo" do
::DatabaseCleaner::Mongo = double("mongo module") unless defined? ::DatabaseCleaner::Mongo
expect(subject.orm_module(:mongo)).to eq DatabaseCleaner::Mongo
stub_const "DatabaseCleaner::Mongo", mod
expect(subject.orm_module(:mongo)).to eq mod
end
it "should return DatabaseCleaner::CouchPotato for :couch_potato" do
::DatabaseCleaner::CouchPotato = double("cp module") unless defined? ::DatabaseCleaner::CouchPotato
expect(subject.orm_module(:couch_potato)).to eq DatabaseCleaner::CouchPotato
stub_const "DatabaseCleaner::CouchPotato", mod
expect(subject.orm_module(:couch_potato)).to eq mod
end
it "should return DatabaseCleaner::Neo4j for :neo4j" do
::DatabaseCleaner::Neo4j = double("nj module") unless defined? ::DatabaseCleaner::Neo4j
expect(subject.orm_module(:neo4j)).to eq DatabaseCleaner::Neo4j
stub_const "DatabaseCleaner::Neo4j", mod
expect(subject.orm_module(:neo4j)).to eq mod
end
end
end

View file

@ -2,27 +2,27 @@ require "database_cleaner/null_strategy"
module DatabaseCleaner
describe NullStrategy do
it 'responds to .start' do
expect { NullStrategy.start }.not_to raise_error
it 'responds to .start' do
expect { NullStrategy.start }.not_to raise_error
end
it 'responds to .clean' do
expect { NullStrategy.clean }.not_to raise_error
end
describe '.cleaning' do
it 'fails without a block' do
expect { NullStrategy.cleaning }.to raise_error(LocalJumpError)
end
it 'responds to .clean' do
expect { NullStrategy.clean }.not_to raise_error
end
it 'no-ops with a block' do
effect = double
expect(effect).to receive(:occur).once
describe '.cleaning' do
it 'fails without a block' do
expect { NullStrategy.cleaning }.to raise_error(LocalJumpError)
end
it 'no-ops with a block' do
effect = double
expect(effect).to receive(:occur).once
NullStrategy.cleaning do
effect.occur
end
NullStrategy.cleaning do
effect.occur
end
end
end
end
end