added DatabaseCleaner() method to return a strategy

This commit is contained in:
Ben Mabey 2009-03-04 22:11:58 -07:00
parent 0b39edc8a4
commit 43ad34fc2b
3 changed files with 28 additions and 2 deletions

View File

@ -1,2 +1,6 @@
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))
require 'database_cleaner/configuration'
def DatabaseCleaner(*args)
DatabaseCleaner.create_strategy(*args)
end

View File

@ -17,12 +17,20 @@ module DatabaseCleaner
end
class << self
def create_strategy(*args)
strategy, *strategy_args = args
orm_strategy(strategy).new(*strategy_args)
end
def strategy=(args)
strategy, *strategy_args = args
if strategy.is_a?(Symbol)
@strategy = orm_strategy(strategy).new(*strategy_args)
else
@strategy = create_strategy(*args)
elsif strategy_args.empty?
@strategy = strategy
else
raise ArgumentError, "You must provide a strategy object, or a symbol for a know strategy along with initialization params."
end
end

View File

@ -10,6 +10,7 @@ describe DatabaseCleaner do
# need to add one for each ORM that we load in the spec helper...
end
after(:all) do
Object.send(:remove_const, 'ActiveRecord') if defined?(::ActiveRecord) #want to make sure we have the real one...
ActiveRecord = TempAR
end
@ -20,6 +21,15 @@ describe DatabaseCleaner do
DatabaseCleaner.orm = nil
end
describe ".create_strategy ( DatabaseCleaner() )" do
it "should initialize and return the appropirate strategy based on the ORM adapter detected" do
DatabaseCleaner::ActiveRecord::Transaction.should_receive(:new).with('options' => 'hash')
result = DatabaseCleaner(:transaction, {'options' => 'hash'})
result.should == @strategy
end
end
describe ".strategy=" do
it "should initialize the appropirate strategy based on the ORM adapter detected" do
DatabaseCleaner::ActiveRecord::Transaction.should_receive(:new).with('options' => 'hash')
@ -47,6 +57,10 @@ describe DatabaseCleaner do
DatabaseCleaner.strategy = :transaction
end
it "should raise an error when multiple args is passed in and the first is not a symbol" do
running { DatabaseCleaner.strategy=Object.new, {:foo => 'bar'} }.should raise_error(ArgumentError)
end
it "should raise an error when the specified strategy is not found" do
running { DatabaseCleaner.strategy = :foo }.should raise_error(DatabaseCleaner::UnknownStrategySpecified)
end