Created
January 21, 2013 21:00
-
-
Save supernullset/4589331 to your computer and use it in GitHub Desktop.
Multi-Database DatabaseCleaner setup
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
RSpec.configure do |config| | |
config.before :suite do | |
# set up a global cleaning strategy | |
DatabaseCleaner.clean_with(:truncation) | |
# AR specific strategy | |
DatabaseCleaner[:active_record].strategy = :truncation | |
# Explicitly tell DatabaseCleaner what connection is AR | |
DatabaseCleaner[:active_record, connection: :pediafed_test] | |
# Mongoid specific strategy | |
DatabaseCleaner[:mongoid].strategy = :truncation | |
# Explicitly tell DatabaseCleaner what connection is MongoDB | |
DatabaseCleaner[:mongoid, connection: :pediafed_test] | |
end | |
config.before :each do | |
DatabaseCleaner.start | |
end | |
config.after :each do | |
DatabaseCleaner.clean | |
end | |
end |
Very helpful, thanks for sharing!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why?
After discovering a huge problem in my tests as I converted a number of the Pediafed models over to ActiveRecord/Postgres; Don and I discovered that DatabaseCleaner was not aware of how to handle multiple datastores from the get go, and was defaulting to the first one it encountered: AR.
Above is the solution. This is well documented in DatabaseCleaner's Documentation, but I figured I would document it for our future endeavors.