LoginSignup
4
0

More than 3 years have passed since last update.

【Tips】Rails の Minitest 実行時に想定外のデータベース名になって ConnectionError となる原因と対処法

Last updated at Posted at 2020-02-24

問題

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
  • 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などとついたデータベースを作成することでも解決可能。

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0