Don't clean it if nothing can be cleaned.

This commit is contained in:
David Kim 2011-12-16 17:24:55 +10:30
parent 7d4b21a1fa
commit f921a26b25
2 changed files with 14 additions and 0 deletions

View File

@ -14,6 +14,8 @@ module DatabaseCleaner::ActiveRecord
def clean
return unless connection_klass.connection.open_transactions > 0
connection_klass.connection.rollback_db_transaction
if connection_klass.connection.respond_to?(:decrement_open_transactions)

View File

@ -38,12 +38,17 @@ module DatabaseCleaner
describe "#clean" do
it "should start a transaction" do
connection.should_receive(:open_transactions).and_return(1)
connection.stub!(:decrement_open_transactions)
connection.should_receive(:rollback_db_transaction)
Transaction.new.clean
end
it "should decrement open transactions if possible" do
connection.should_receive(:open_transactions).and_return(1)
connection.stub!(:respond_to?).with(:decrement_open_transactions).and_return(true)
connection.stub!(:rollback_db_transaction)
@ -51,7 +56,14 @@ module DatabaseCleaner
Transaction.new.clean
end
it "should not try to decrement or rollback if open_transactions is 0 for whatever reason" do
connection.should_receive(:open_transactions).and_return(0)
Transaction.new.clean
end
it "should decrement connection via ActiveRecord::Base if connection won't" do
connection.should_receive(:open_transactions).and_return(1)
connection.stub!(:respond_to?).with(:decrement_open_transactions).and_return(false)
connection.stub!(:rollback_db_transaction)