Adding before / after blocks to every spec

Folks,

I was wondering if someone would be able to help me out with a quandry.

I’m trying to write an rspec plugin module for merb that will run all
specs within an ActiveRecord (initially) database transaction - to
give functionality similar to rails transactional fixtures.

Here is the module:

module Merb
module Test
module TransactionalSpecs
def begin_transaction
ActiveRecord::Base.send :increment_open_transactions
ActiveRecord::Base.connection.begin_db_transaction
end

  def rollback_transaction
    if Thread.current['open_transactions'] != 0
      ActiveRecord::Base.connection.rollback_db_transaction
      Thread.current['open_transactions'] = 0
    end
  end

  def self.included(base)
    base.before(:each) { begin_transaction }
    base.after(:each) { rollback_transaction }
  end
end

end
end

and I’m including it in a spec_helper like so:

Spec::Runner.configure do |config|
config.include(Merb::Test::TransactionalSpecs)
end

Unfortunately this results in every spec throwing a
SQLite3::SQLException “SQL logic error or missing database”.

if I remove the self.included method from the module, and just add:

before(:each) { begin_transaction }
after(:each) { rollback_transaction }

within the describe block it works perfectly.

Does anybody know what might be causing this, or a way around it?

Thanks for your help.

Steve