Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 25th, 2012  |  syntax: None  |  size: 0.98 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # js-request tests cannot use transactions, since they happen in another process
  2. # so if we are in js request tests, use the real test database and then clean it afterwards
  3.  
  4. module ActiveRecord
  5.   module TestFixtures
  6.     def run_in_transaction_with_js?
  7.       return false if $disable_transactions
  8.       run_in_transaction_without_js?
  9.     end
  10.     alias_method_chain :run_in_transaction?, :js
  11.   end
  12. end
  13.  
  14. RSpec.configure do |config|
  15.   config.around do |block|
  16.     if example.metadata[:js]
  17.       $disable_transactions = true
  18.  
  19.       block.call
  20.       $disable_transactions = false
  21.  
  22.       # kill everything
  23.       DatabaseCleaner.clean_with(:truncation)
  24.  
  25.       # reload all fixtures for next test
  26.       ActiveRecord::Fixtures.reset_cache
  27.       fixtures_folder = File.join(Rails.root, 'test', 'fixtures')
  28.       fixtures = Dir[File.join(fixtures_folder, '*.yml')].map {|f| File.basename(f, '.yml') }
  29.       ActiveRecord::Fixtures.create_fixtures(fixtures_folder, fixtures)
  30.     else
  31.       block.call
  32.     end
  33.   end
  34. end