LoginSignup
0
1

More than 3 years have passed since last update.

rails test がデータベースの参照エラーで失敗する

Posted at

経緯

Docker の中で Rails と MySQL を立て、 rails test でテストを実行しようとしたところ、エラーとなりました。

docker-compose.yml
services:
  mysql_test:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: aaa
      MYSQL_USER: user
      MYSQL_PASSWORD: password
      TZ: 'Asia/Tokyo'
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
$ rails test test/integration/aaa_test.rb
Error:
AaaTest#aaa:
ActiveRecord::ConnectionNotEstablished: Access denied for user 'user'@'%' to database 'aaa-1'

エラー内容を見ると aaa-1 を参照しており、そのデータベースはないのでエラーとなっています。

解決策

test_helper.rb にある parallelize(workers: :number_of_processors) の影響なので、 PARALLEL_WORKERS を変更して suffix がつかないようにします。

$ PARALLEL_WORKERS=1 rails test test/integration/aaa_test.rb

これで動作します。

0
1
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
0
1