質問内容
Docker上でRspecを走らせようとしたところエラーが発生してしまいました。
MySQLとの接続が上手くいっていないようです。
database.ymlとdocker-compose.ymlを見直しても問題があるようには見えず、コンテナとイメージを全て削除して再び起動してもエラーコードの内容は変わりませんでした。
アドバイス等ありましたら頂けると幸いです。
よろしくお願いします。
開発環境
Ruby 2.5.1
Rails 5.2.4.3
Mac OS
#エラーコード
console
1~@yk golfour_aws % bundle exec rspec 2An error occurred while loading ./spec/system/training_posts_spec.rb. 3Failure/Error: ActiveRecord::Migration.maintain_test_schema! 4 5Mysql2::Error::ConnectionError: 6 Unknown MySQL server host 'db-test' (0) 7# ./spec/rails_helper.rb:28:in `<top (required)>' 8# ./spec/system/training_posts_spec.rb:1:in `require' 9# ./spec/system/training_posts_spec.rb:1:in `<top (required)>' 10No examples found. 11 12Finished in 0.00004 seconds (files took 2.88 seconds to load) 130 examples, 0 failures, 1 error occurred outside of examples
関連ファイル
docker-compose.yml:
version: '3' services: db: image: mysql:8.0 command: --default-authentication-plugin=mysql_native_password environment: MYSQL_ROOT_PASSWORD: password MYSQL_DATABASE: golfour_development MYSQL_USER: yuki MYSQL_PASSWORD: password TZ: Asia/Tokyo volumes: - ./mysql/mysql_data:/var/lib/mysql - ./logs:/var/log/mysql - ./mysql/my.cnf:/etc/mysql/conf.d/my.cnf ports: - "4306:3306" db-test: image: mysql:8.0 command: --default-authentication-plugin=mysql_native_password environment: MYSQL_ROOT_PASSWORD: password MYSQL_DATABASE: golfour_test MYSQL_USER: yuki MYSQL_PASSWORD: password TZ: Asia/Tokyo volumes: - ./mysql/my.cnf:/etc/mysql/conf.d/my.cnf tmpfs: - /var/lib/mysql - /var/log/mysql ports: - "5306:3306" web: build: context: . dockerfile: Dockerfile command: /bin/sh -c "rm -f /workdir/tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" tty: true stdin_open: true depends_on: - db - db-test ports: - "3000:3000" volumes: - .:/workdir
database.yml:
default: &default adapter: mysql2 encoding: utf8 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> development: <<: *default database: golfour_development username: yuki password: password host: db socket: /tmp/mysql.sock test: <<: *default database: golfour_test username: yuki password: password host: db-test socket: /tmp/mysql.sock production: <<: *default database: <%= ENV['DB_NAME'] %> username: <%= ENV['DB_USERNAME'] %> password: <%= ENV['DB_PASSWORD'] %> host: <%= ENV['DB_HOSTNAME'] %>
rails.helper:
# This file is copied to spec/ when you run 'rails generate rspec:install' require 'spec_helper' ENV['RAILS_ENV'] ||= 'test' require File.expand_path('../config/environment', __dir__) # Prevent database truncation if the environment is production abort("The Rails environment is running in production mode!") if Rails.env.production? require 'rspec/rails' # Add additional requires below this line. Rails is not loaded until this point! # Requires supporting ruby files with custom matchers and macros, etc, in # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are # run as spec files by default. This means that files in spec/support that end # in _spec.rb will both be required and run as specs, causing the specs to be # run twice. It is recommended that you do not name files matching this glob to # end with _spec.rb. You can configure this pattern with the --pattern # option on the command line or in ~/.rspec, .rspec or `.rspec-local`. # # The following line is provided for convenience purposes. It has the downside # of increasing the boot-up time by auto-requiring all files in the support # directory. Alternatively, in the individual `*_spec.rb` files, manually # require only the support files necessary. # # Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f } # Checks for pending migrations and applies them before tests are run. # If you are not using ActiveRecord, you can remove these lines. begin ActiveRecord::Migration.maintain_test_schema! rescue ActiveRecord::PendingMigrationError => e puts e.to_s.strip exit 1 end RSpec.configure do |config| # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures config.fixture_path = "#{::Rails.root}/spec/fixtures" # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, remove the following line or assign false # instead of true. config.use_transactional_fixtures = true # You can uncomment this line to turn off ActiveRecord support entirely. # config.use_active_record = false # RSpec Rails can automatically mix in different behaviours to your tests # based on their file location, for example enabling you to call `get` and # `post` in specs under `spec/controllers`. # # You can disable this behaviour by removing the line below, and instead # explicitly tag your specs with their type, e.g.: # # RSpec.describe UsersController, type: :controller do # # ... # end # # The different available types are documented in the features, such as in # https://relishapp.com/rspec/rspec-rails/docs config.infer_spec_type_from_file_location! # Filter lines from Rails gems in backtraces. config.filter_rails_from_backtrace! # arbitrary gems may also be filtered via: # config.filter_gems_from_backtrace("gem name") end
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/21 05:52
2020/08/21 06:43
2020/08/21 09:10
2020/08/21 09:12