Created
June 9, 2012 18:14
-
-
Save maxim/2902072 to your computer and use it in GitHub Desktop.
Making after_commit play nice with use_transactional_fixtures.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is an example of how to use database_cleaner gem and | |
# RSpec tags to make `after_commit` hook play nice with | |
# `use_transactional_fixtures`. | |
# Simply mark the specs that use after_commit with | |
# `:uses_after_commit` tag. | |
# ... | |
require 'database_cleaner' | |
RSpec.configure do |c| | |
# ... | |
c.treat_symbols_as_metadata_keys_with_true_values = true | |
c.use_transactional_fixtures = true | |
# Tag "uses_after_commit" helps when after_commit hook is expected | |
# to fire in a spec. It would never fire because of having enabled | |
# use_transactional_fixtures. It waits for transaction to end. The | |
# workaround disables transaction-wrapping for the tagged spec and | |
# instead uses a DatabaseCleaner strategy to wipe the tables here. | |
c.around(:each, :uses_after_commit) do |example| | |
_orig_use_transactional_fixtures = use_transactional_fixtures | |
self.use_transactional_fixtures = false | |
DatabaseCleaner.clean_with(:truncation) | |
example.call | |
DatabaseCleaner.clean_with(:truncation) | |
self.use_transactional_fixtures = _orig_use_transactional_fixtures | |
end | |
# ... | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment