Module: Prefactory::Transactionality

Defined in:
lib/rspec/core/prefactory.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# 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 => original_caller }
      if args.last.is_a?(Hash)
        args.last.merge!()
      else
        args << 
      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