問題
rails test
実行時にデータベース名末尾に-0
-1
などが付きアクセスが拒否されてしまう。
入力
rails test
出力
ERROR["test_【テストの名前】", #<Minitest::Reporters::Suite:0x******** @name="【テストクラスの名前】">, 0.143750600022031]
test_n【テストの名前】#【テストクラスの名前】 (0.14s)
Minitest::UnexpectedError: Mysql2::Error::ConnectionError: Access denied for user '********'@'%' to database '【テスト用データベースに設定した名前】-0'
ERROR["test_【テストの名前】", #<Minitest::Reporters::Suite:0x******** @name="【テストクラスの名前】">, 0.143750600022031]
test_n【テストの名前】#【テストクラスの名前】 (0.21s)
Minitest::UnexpectedError: Mysql2::Error::ConnectionError: Access denied for user '********'@'%' to database '【テスト用データベースに設定した名前】-1'
ERROR["test_【テストの名前】", #<Minitest::Reporters::Suite:0x******** @name="【テストクラスの名前】">, 0.143750600022031]
test_n【テストの名前】#【テストクラスの名前】 (0.31s)
Minitest::UnexpectedError: Mysql2::Error::ConnectionError: Access denied for user '********'@'%' to database '【テスト用データベースに設定した名前】-2'
...
環境
- Windows 10
- Ubuntu 18.04 LTS
- anyenv 1.1.1
- rbenv 1.1.2-20-g143b2c9
- ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux]
- Rails 6.0.2.1
- ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux]
- rbenv 1.1.2-20-g143b2c9
- MySQL
原因
test/test_helper.rb
で並列実行設定が行われているため複数のデータベースにアクセスされてしまう。
解決方法
並列化設定をコメントアウトする。
test/test_helper.rb
class ActiveSupport::TestCase
# 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
並列化設定を行っているparallelize
メソッドの説明はこちら。
補足
おそらく-0
-1
などとついたデータベースを作成することでも解決可能。