I am working on the tests for an app and just now got beyond 50 tests, which means the parallel testing kicked in. With it I currently get error messages telling me ActiveRecord::ConnectionNotEstablished: Access denied for user 'user'@'localhost' to database 'test_DB-0', 'test_DB-1', 'test_DB-2' and so on. That is because I just have one test-DB specified in my database.yml.
I read in the rails guides that apparently I have to use before and after actions in the test_helper.rb
ENV["RAILS_ENV"] ||= "test"
require_relative "../config/environment"
require "rails/test_help"
require "minitest/autorun"
require "minitest/reporters"
Minitest::Reporters.use!
class ActiveSupport::TestCase
parallelize_setup do |worker|
# setup databases
end
parallelize_teardown do |worker|
# setup databases
end
# Run tests in parallel with specified workers
parallelize(workers: :number_of_processors)
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
But how do I do this and can I leave the data for my old test-db in database.yml? Can I get around creating the DBs by hand by using these before and after actions?
Thank you in advance!
Edit
The test section of my database.yml is:
test:
adapter: mysql2
database: <%= ENV["testDB"] %>
username: <%= ENV["testDB-username"] %>
password: <%= ENV["testDB-password"] %>
host: localhost
encoding: utf8
collation: utf8_unicode_ci
The testDB-username on localhost has access to the testDB, but has no global root rights.
parallelize(workers: 1)do your tests work? Thing is It should work automatically provided your database credentials have proper rights to create new databases. Rails tests should handle all these databases for you, you shouldn't have to specify them in database.ymlparallelize(with: :threads)but would like to configure the multiple DB setting for performance reasons.