前提・実現したいこと
つい先程までは成功していたのですが突然rspecによるテストが実行できなくなりました。
Ruby: 2.6.5
Rails: 6.0.3.2 (5.2.4.2のときでも似たようなエラーが発生したためrails6の問題ではないとは思います)
mysql: 8.0.19
発生している問題・エラーメッセージ
Railsでアプリを作成中、bundle exec rspec
を行ったところ
DEPRECATION WARNING: #fog_provider is deprecated and has no effect (called from block in <main> at /Users/futo/Portfolio/stelle_app/config/initializers/carrierwave.rb:7) An error occurred while loading ./spec/models/like_spec.rb. Failure/Error: validates :password, presence: true, length: { minimum: 6 }, on: create ActiveRecord::StatementInvalid: Mysql2::Error: Table 'stelle_app_test.users' doesn't exist # ./app/models/user.rb:45:in `<class:User>' # ./app/models/user.rb:23:in `<main>' # ./config/routes.rb:4:in `block in <main>' # ./config/routes.rb:1:in `<main>' # ./config/environment.rb:5:in `<top (required)>' # ./spec/rails_helper.rb:4:in `require' # ./spec/rails_helper.rb:4:in `<top (required)>' # ./spec/models/like_spec.rb:22:in `require' # ./spec/models/like_spec.rb:22:in `<top (required)>' # ------------------ # --- Caused by: --- # Mysql2::Error: # Table 'stelle_app_test.users' doesn't exist # ./app/models/user.rb:45:in `<class:User>' An error occurred while loading ./spec/models/post_spec.rb. Failure/Error: require File.expand_path('../config/environment', __dir__) FrozenError: can't modify frozen Array # ./config/environment.rb:5:in `<main>' # ./spec/rails_helper.rb:4:in `<main>' # ./spec/models/post_spec.rb:24:in `<main>' 省略 An error occurred while loading ./spec/requests/users_request_spec.rb. Failure/Error: require File.expand_path('../config/environment', __dir__) FrozenError: can't modify frozen Array # ./config/environment.rb:5:in `<main>' # ./spec/rails_helper.rb:4:in `<main>' # ./spec/requests/users_request_spec.rb:1:in `<main>' No examples found. Finished in 0.00003 seconds (files took 1.19 seconds to load) 0 examples, 0 failures, 9 errors occurred outside of examples
というエラーになってしまいました。stelle_appというアプリケーションなのですが、どうやらstelle_app_testに問題があるみたいです。
試したこと
まず、マイグレーションファイルファイルがおかしくないか確認。
❯ rails db:migrate:status database: stelle_app_development Status Migration ID Migration Name -------------------------------------------------- up 20200630041519 Devise create users up 20200630052243 Create prefectures up 20200630052413 Create posts up 20200630053527 Create likes up 20200630053907 Create relationships
問題はなさそうです。
次にdbをリセットしてみる。
❯ rails db:migrate:reset Dropped database 'stelle_app_development' Dropped database 'stelle_app_test' Created database 'stelle_app_development' Created database 'stelle_app_test' 省略 == 20200630053907 CreateRelationships: migrating ============================== -- create_table(:relationships) -> 0.0529s == 20200630053907 CreateRelationships: migrated (0.0530s) ===================== Model files unchanged.
最後のModel files unchanged.が気になりますがエラーも起こらずrails s
をしてlocalhostに接続してみても問題なくアプリは動く。。。
テスト環境に問題がありそう。。。
❯ rails db:migrate RAILS_ENV=test DEPRECATION WARNING: #fog_provider is deprecated and has no effect (called from block in <main> at /Users/futo/Portfolio/stelle_app/config/initializers/carrierwave.rb:7) rails aborted! ActiveRecord::StatementInvalid: Mysql2::Error: Table 'stelle_app_test.users' doesn't exist
❯ rails db:migrate --trace ** Invoke db:migrate (first_time) 省略 ** Invoke environment ** Execute annotate_models Model files unchanged.
mysql> show databases; +------------------------+ | Database | +------------------------+ | information_schema | | mysql | | performance_schema | | stelle_app_development | | stelle_app_test | | sys | +------------------------+ 6 rows in set (0.01 sec)
❯ rails db:create:all Database 'stelle_app_development' already exists Database 'stelle_app_test' already exists Access denied for user 'stelle_app'@'localhost' (using password: NO) Couldn't create 'stelle_app_production' database. Please check your configuration. rails aborted! Mysql2::Error::ConnectionError: Access denied for user 'stelle_app'@'localhost' (using password: NO) bin/rails:4:in `<main>' Tasks: TOP => db:create:all (See full trace by running task with --trace)
色々試してみましたがそれらしい原因が見つかりませんでした。
該当のソースコード
Gemfile
1source 'https://rubygems.org' 2git_source(:github) { |repo| "https://github.com/#{repo}.git" } 3 4ruby '2.6.5' 5 6 7gem 'rails', '>= 5.2.4.2' 8gem 'mysql2', '>= 0.4.4', '< 0.6.0' 9gem 'turbolinks', '~> 5' 10gem 'puma', '~> 3.11' 11gem 'sass-rails', '~> 5.0' 12gem 'uglifier', '>= 1.3.0' 13gem 'coffee-rails', '~> 5.0.0' 14gem 'jbuilder', '~> 2.5' 15gem 'bcrypt', '~> 3.1.7' 16gem 'bootsnap', '>= 1.1.0', require: false 17 18 19# ---以下追加--- 20gem 'font-awesome-rails' 21gem 'bootstrap', '~> 4.3.1' 22gem 'bootstrap-honoka-rails' , '~> 4.3.1' 23gem 'jquery-rails' 24gem 'devise' 25gem 'rails-i18n' 26gem 'devise-i18n' 27gem 'devise-i18n-views' 28gem 'faker' 29gem 'carrierwave' 30gem 'mini_magick', '4.8.0' 31gem 'fog-aws' 32gem 'dotenv-rails' 33gem 'ransack' 34 35 36group :development, :test do 37 gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] 38 39 # ---以下追加--- 40 gem 'rspec-rails' 41 gem 'factory_bot_rails' 42 gem 'spring-commands-rspec' 43end 44 45 46group :development do 47 gem 'web-console', '>= 3.3.0' 48 gem 'listen', '>= 3.0.5', '< 3.2' 49 50 # ---以下追加--- 51 gem 'spring' 52 gem 'spring-watcher-listen', '~> 2.0.0' 53 gem 'annotate' 54 gem 'bullet' 55 gem 'rails-flog', require: 'flog' 56end 57 58 59group :test do 60 gem 'capybara', '>= 2.15' 61 gem 'webdrivers', '~> 3.0' 62 63 # ---以下追加--- 64 gem 'selenium-webdriver' 65 gem 'rspec_junit_formatter' 66end 67 68 69# Windows does not include zoneinfo files, so bundle the tzinfo-data gem 70gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
database.yml
1# MySQL. Versions 5.1.10 and up are supported. 2# 3# Install the MySQL driver 4# gem install mysql2 5# 6# Ensure the MySQL gem is defined in your Gemfile 7# gem 'mysql2' 8# 9# And be sure to use new-style password hashing: 10# https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html 11# 12default: &default 13 adapter: mysql2 14 encoding: utf8 15 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 16 username: root 17 password: 18 socket: /tmp/mysql.sock 19 20development: 21 <<: *default 22 database: stelle_app_development 23 24# Warning: The database defined as "test" will be erased and 25# re-generated from your development database when you run "rake". 26# Do not set this db to the same as development or production. 27test: 28 <<: *default 29 database: stelle_app_test 30 31# As with config/secrets.yml, you never want to store sensitive information, 32# like your database password, in your source code. If your source code is 33# ever seen by anyone, they now have access to your database. 34# 35# Instead, provide the password as a unix environment variable when you boot 36# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database 37# for a full rundown on how to provide these environment variables in a 38# production deployment. 39# 40# On Heroku and other platform providers, you may have a full connection URL 41# available as an environment variable. For example: 42# 43# DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase" 44# 45# You can use this database configuration with: 46# 47# production: 48# url: <%= ENV['DATABASE_URL'] %> 49# 50production: 51 <<: *default 52 database: stelle_app_production 53 username: stelle_app 54 password: <%= ENV['STELLE_APP_DATABASE_PASSWORD'] %> 55
Ruby
1./config/enviroment.rb 2 3# Load the Rails application. 4require_relative 'application' 5 6# Initialize the Rails application. 7Rails.application.initialize! #該当部分 8
Ruby
1./spec/rails_helper.rb 2 3require 'spec_helper' 4ENV['RAILS_ENV'] ||= 'test' 5require File.expand_path('../config/environment', __dir__) #該当部分 6# Prevent database truncation if the environment is production 7abort("The Rails environment is running in production mode!") if Rails.env.production? 8require 'rspec/rails'
Ruby
1 2./spec/requests/users_request_spec 3 4require 'rails_helper' #該当部分 5 6RSpec.describe "Users", type: :request do 7 8 describe "GET /show" do 9 it "returns http success" do 10 get "/users/show" 11 expect(response).to have_http_status(:success) 12 end 13 end 14 15 describe "GET /index" do 16 it "returns http success" do 17 get "/users/index" 18 expect(response).to have_http_status(:success) 19 end 20 end 21 22 describe "GET /edit" do 23 it "returns http success" do 24 get "/users/edit" 25 expect(response).to have_http_status(:success) 26 end 27 end 28end
なにか情報がありましたらお願いします。
回答1件
あなたの回答
tips
プレビュー