I'd like to use zeus (0.13.3) to preload my rails environment for an rails (3.2.11) app on ruby 2.0.0 and use database_cleaner (0.9.1) to clean the databases. This is working fine if as long as I use only one mysql database. In the app we have to use two different mysql databases. Edit: What needs to be mentioned is, that I use the shared connection hack, which is described here.
Running my specs without using zeus is working like expected. But if I use it, every test is failing in the moment when a record is created with the error:
undefined method `query_options' for nil:NilClass
How can this be solved?
The setup is like that:
config/database.yml
test:
database: app_test
...
test_second_db:
database: other_test
...
In an abstract base class for all the models using the second connection we do
models/second_db/base.rb
module SecondDB
class Base < ActiveRecord::Base
establish_connection "#{Rails.env}_second_db"
self.abstract_class = true
end
end
In my spec_helper.rb file I setup the database_cleaner:
spec/spec_helper.rb
require 'database_cleaner'
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner[:active_record,{:connection => :test_second_db}].strategy = :transaction
end
config.before(:each) do
DatabaseCleaner.start
DatabaseCleaner[:active_record,{:connection => :test_second_db}].start
end
config.after(:each) do
DatabaseCleaner.clean
DatabaseCleaner[:active_record,{:connection => :test_second_db}].clean
end
end
edit:
spec/support/shared_connection.rb
class ActiveRecord::Base
mattr_accessor :shared_connection
@@shared_connection = nil
def self.connection
@@shared_connection || retrieve_connection
end
end
spec/support/shared_connection.rb