allow setting of orm and skip autodetect

This commit is contained in:
Ben Mabey 2009-03-04 14:00:20 -07:00
parent 27e8b337f7
commit 9fa3eb0818
2 changed files with 22 additions and 7 deletions

View file

@ -26,6 +26,10 @@ module DatabaseCleaner
end end
end end
def orm=(orm_string)
@orm = orm_string
end
def start def start
strategy.start strategy.start
end end
@ -50,6 +54,7 @@ module DatabaseCleaner
def orm def orm
@orm ||=begin
if defined? ::ActiveRecord if defined? ::ActiveRecord
'active_record' 'active_record'
elsif defined? ::DataMapper elsif defined? ::DataMapper
@ -58,6 +63,7 @@ module DatabaseCleaner
raise NoORMDetected, "No known ORM was detected! Is ActiveRecord or DataMapper loaded?" raise NoORMDetected, "No known ORM was detected! Is ActiveRecord or DataMapper loaded?"
end end
end end
end
def orm_module def orm_module

View file

@ -7,6 +7,7 @@ describe DatabaseCleaner do
DatabaseCleaner::ActiveRecord::Transaction.stub!(:new).and_return(@strategy = mock('strategy')) DatabaseCleaner::ActiveRecord::Transaction.stub!(:new).and_return(@strategy = mock('strategy'))
Object.const_set('ActiveRecord', "just mocking out the constant here...") unless defined?(::ActiveRecord) Object.const_set('ActiveRecord', "just mocking out the constant here...") unless defined?(::ActiveRecord)
DatabaseCleaner.strategy = nil DatabaseCleaner.strategy = nil
DatabaseCleaner.orm = nil
end end
describe ".strategy=" do describe ".strategy=" do
@ -16,6 +17,7 @@ describe DatabaseCleaner do
Object.send(:remove_const, 'ActiveRecord') Object.send(:remove_const, 'ActiveRecord')
Object.const_set('DataMapper', "just mocking out the constant here...") Object.const_set('DataMapper', "just mocking out the constant here...")
DatabaseCleaner.orm = nil
DatabaseCleaner::DataMapper::Transaction.should_receive(:new).with(no_args) DatabaseCleaner::DataMapper::Transaction.should_receive(:new).with(no_args)
DatabaseCleaner.strategy = :transaction DatabaseCleaner.strategy = :transaction
@ -28,6 +30,13 @@ describe DatabaseCleaner do
running { DatabaseCleaner.strategy = :transaction }.should raise_error(DatabaseCleaner::NoORMDetected) running { DatabaseCleaner.strategy = :transaction }.should raise_error(DatabaseCleaner::NoORMDetected)
end end
it "should use the strategy version of the ORM specified with #orm=" do
DatabaseCleaner.orm = 'data_mapper'
DatabaseCleaner::DataMapper::Transaction.should_receive(:new)
DatabaseCleaner.strategy = :transaction
end
it "should raise an error when the specified strategy is not found" do it "should raise an error when the specified strategy is not found" do
running { DatabaseCleaner.strategy = :foo }.should raise_error(DatabaseCleaner::UnknownStrategySpecified) running { DatabaseCleaner.strategy = :foo }.should raise_error(DatabaseCleaner::UnknownStrategySpecified)
end end