module Prefactory::Transactionality

Public Class Methods

included(base) click to toggle source
# File lib/rspec/core/prefactory.rb, line 141
def self.included(base)
  require 'prefactory/active_record_integration'
  base.extend RSpecAroundAll
  # Wrap outermost describe block in a transaction, so before(:all) data is rolled back at the end of this suite.
  base.before(:all) do
    clear_prefactory_memoizations
  end
  base.around(:all) do |group|
    ActiveRecord::Base.with_disposable_transaction { group.run_examples }
  end
  base.after(:all) do
    clear_prefactory_memoizations
  end

  # Wrap each example in a transaction, instead of using Rails' transactional
  # fixtures, which does not support itself being wrapped in an outermost transaction.
  base.around(:each) do |example|
    clear_prefactory_memoizations
    ActiveRecord::Base.with_disposable_transaction { example.run }
    clear_prefactory_memoizations
  end

  # Wrap each ExampleGroup in a transaction, so group-level before(:all) settings
  # are scoped only to the group.
  base.instance_eval do
    def describe_with_transaction(*args, &block)
      original_caller = caller
      modified_block = proc do
        instance_eval do
          before(:all) { clear_prefactory_memoizations }
          around(:all) do |group|
            ActiveRecord::Base.with_disposable_transaction { group.run_examples }
          end
          after(:all) { clear_prefactory_memoizations }
        end
        instance_eval(&block)
      end

      caller_metadata = { :caller => original_caller }
      if args.last.is_a?(Hash)
        args.last.merge!(caller_metadata)
      else
        args << caller_metadata
      end

      describe_without_transaction(*args, &modified_block)
    end

    class << self
      alias_method :describe_without_transaction, :describe
      alias_method :describe, :describe_with_transaction
      alias_method :context, :describe
    end
  end
end

Public Instance Methods

describe_with_transaction(*args, &block) click to toggle source
# File lib/rspec/core/prefactory.rb, line 166
def describe_with_transaction(*args, &block)
  original_caller = caller
  modified_block = proc do
    instance_eval do
      before(:all) { clear_prefactory_memoizations }
      around(:all) do |group|
        ActiveRecord::Base.with_disposable_transaction { group.run_examples }
      end
      after(:all) { clear_prefactory_memoizations }
    end
    instance_eval(&block)
  end

  caller_metadata = { :caller => original_caller }
  if args.last.is_a?(Hash)
    args.last.merge!(caller_metadata)
  else
    args << caller_metadata
  end

  describe_without_transaction(*args, &modified_block)
end