Skip to content

Instantly share code, notes, and snippets.

@grosser
Created Jan 13, 2012
Embed
What would you like to do?
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