Skip to content

Instantly share code, notes, and snippets.

@grosser
Created January 13, 2012 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grosser/1608889 to your computer and use it in GitHub Desktop.
Save grosser/1608889 to your computer and use it in GitHub Desktop.
Using no transactions for js request specs (may not work, just a prototype I do not want to loose...)
# js-request tests cannot use transactions, since they happen in another process
# so if we are in js request tests, use the real test database and then clean it afterwards
module ActiveRecord
module TestFixtures
def run_in_transaction_with_js?
return false if $disable_transactions
run_in_transaction_without_js?
end
alias_method_chain :run_in_transaction?, :js
end
end
RSpec.configure do |config|
config.around do |block|
if example.metadata[:js]
$disable_transactions = true
block.call
$disable_transactions = false
# kill everything
DatabaseCleaner.clean_with(:truncation)
# reload all fixtures for next test
ActiveRecord::Fixtures.reset_cache
fixtures_folder = File.join(Rails.root, 'test', 'fixtures')
fixtures = Dir[File.join(fixtures_folder, '*.yml')].map {|f| File.basename(f, '.yml') }
ActiveRecord::Fixtures.create_fixtures(fixtures_folder, fixtures)
else
block.call
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment