LoginSignup
4
1

More than 3 years have passed since last update.

RSpecでFailure/Error: require File.expand_path('../config/environment', __dir__)が出たときの対処法

Last updated at Posted at 2020-10-10

何が起こったか

rspecコマンドでテストを実行したら,今までなんの問題もなく実行できていたのに突如タイトルのようなエラーが起きました
結果的にbundle updateのせいとわかりました.何気なくbundle updateをしていた身としては「何もしてないのにエラー出た」な気分でした

以下rspecコマンドを実行した時に出力された結果です

$ rspec spec/models/task_spec.rb 

 An error occurred while loading ./spec/models/task_spec.rb.
 Hint: Install the `did_you_mean` gem in order to provide suggestions for similarly named files.
 Failure/Error: require File.expand_path('../config/environment', __dir__)

 LoadError:
   cannot load such file -- public_suffix
 # ./config/application.rb:7:in `<top (required)>'
 # ./config/environment.rb:2:in `require_relative'
 # ./config/environment.rb:2:in `<top (required)>'
 # ./spec/rails_helper.rb:5:in `<top (required)>'
 # ./spec/models/task_spec.rb:1:in `<top (required)>'
 No examples found.

 Finished in 0.00005 seconds (files took 1.39 seconds to load)
 0 examples, 0 failures, 1 error occurred outside of examples

解決方法

先に解決方法を書いていきます
このエラーの問題はライブラリのバージョンです
cannot load such file -- public_suffixとあるようにpublic_suffixというライブラリがバージョンの影響なのか読み込めていません
私の場合はエラー時点でpublic_suffix (4.0.6)でした
エラーが出ていない前コミットを見るとpublic_suffix (4.0.5)となっていたので,Gemfile.lockからpublic_suffix (4.0.6)public_suffix (4.0.5)へと変更してbundle installを実行

もう一度rspecでテストを実行すると

$ rspec spec/models/task_spec.rb 

 An error occurred while loading ./spec/models/task_spec.rb.
 Hint: Install the `did_you_mean` gem in order to provide suggestions for similarly named files.
 Failure/Error: require 'rspec/rails'

 LoadError:
   cannot load such file -- minitest/assertions
 # ./spec/rails_helper.rb:8:in `<top (required)>'
 # ./spec/models/task_spec.rb:1:in `<top (required)>'
 No examples found.

 Finished in 0.00013 seconds (files took 2.54 seconds to load)
 0 examples, 0 failures, 1 error occurred outside of examples

と,またエラーが出ましたがcannot load such file -- minitest/assertionsとminitestのバージョンをpublic_suffixと同様に直してあげます
私の場合はminitest (5.14.2)minitest (5.14.1)にしました
またbundle installしてrspecを実行すると

$ rspec spec/models/task_spec.rb 

 Task
   ほげ2

 Finished in 0.00808 seconds (files took 1.99 seconds to load)
 1 example, 0 failures

通った!直った:joy:

原因・解決のためにやったこと

以下,何故このエラーが起こったのか,どんな検索をして解決しようとしたのか書いていきます

問題の本質はFailure/Error: require File.expand_path('../config/environment', __dir__)ではなく,cannot load such file -- public_suffixです
public_suffixというライブラリがあるのですが,それが読み込まれていないことが問題でした
ただGemfile.lockを見るとインストールされています.なのでバージョン違いか?という考えに行きついて解決できました

当初はエラーだと思って検索したのがFailure/Error: require File.expand_path('../config/environment', __dir__)でした
そのためタイトルにも盛り込んでいます.しかし,検索してもしても問題は解決できず,rspec側の問題では無いということしか分かりませんでした

rails newで初期プロジェクトから改めてrspecを導入してもエラーは消えなかったので,このことからもrspecの問題では無いことが明らかでした

6時間くらい格闘してFailure/Error: require File.expand_path('../config/environment', __dir__)は本質的な問題の副次作用だと気がついたのでLoadError: cannot load such file -- public_suffixがエラーの本質なのだと気がつきました
public_suffixがライブラリだと知らなかったのでそう言うエラーなのかと思ってたのも解決に時間がかかった理由です
あとはGemfile.lockをpublic_suffixで検索して,前コミットと見比べてバージョンが違うことに気が付いたので戻してあげたら直ったのが事の顛末です

ちなみにFailure/Error: require File.expand_path('../config/environment', __dir__)で検索しても解決には至らなかったので,同じエラーが出た人のためにこの記事を書きました

教訓

無闇なbundle updateは辞めましょう!
エラーの原因はライブラリのバージョン変化でしたのでいきなりエラーが出たらバージョンを疑いましょう
そもそも,bundle周りの挙動がわからず,なんとなくで使っていたのが一番のダメ要素でした
bundleで痛い目見たのでここの記事で勉強しました
https://qiita.com/lasershow/items/1a048d03ddaaba98171e

付録

public_suffixとminitestのバージョンを直して,rspecが動作するようになったGemfileとGemfile.lockです

Gemfile


ruby '2.6.5'
 gem 'bootstrap', '4.5.2'
 gem 'slim-rails', '3.2.0'
 gem 'html2slim', '0.2.0'
 gem 'enum_help', '0.0.17'

 gem 'rails', '~> 6.0.1'
 gem 'pg', '>= 0.18', '< 2.0'
 gem 'puma', '~> 4.1'
 gem 'sass-rails', '>= 6'
 gem 'webpacker', '~> 4.0'
 gem 'turbolinks', '~> 5'
 gem 'jbuilder', '~> 2.7'
 gem 'bootsnap', '>= 1.4.2'

 group :development, :test do
   gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
 end

 group :development do
   gem 'web-console', '>= 3.3.0'
   gem 'listen', '>= 3.0.5', '< 3.2'
   gem 'spring'
   gem 'spring-watcher-listen', '~> 2.0.0'
 end

 group :test do
   gem 'capybara', '>= 2.15'
   gem 'selenium-webdriver'
   gem 'webdrivers'
   gem 'rspec-rails', '< 4.0.0'
 end

 gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

Gemfile.lock

GEM
  remote: https://rubygems.org/
  specs:
    actioncable (6.0.3.4)
      actionpack (= 6.0.3.4)
      nio4r (~> 2.0)
      websocket-driver (>= 0.6.1)
    actionmailbox (6.0.3.4)
      actionpack (= 6.0.3.4)
      activejob (= 6.0.3.4)
      activerecord (= 6.0.3.4)
      activestorage (= 6.0.3.4)
      activesupport (= 6.0.3.4)
      mail (>= 2.7.1)
    actionmailer (6.0.3.4)
      actionpack (= 6.0.3.4)
      actionview (= 6.0.3.4)
      activejob (= 6.0.3.4)
      mail (~> 2.5, >= 2.5.4)
      rails-dom-testing (~> 2.0)
    actionpack (6.0.3.4)
      actionview (= 6.0.3.4)
      activesupport (= 6.0.3.4)
      rack (~> 2.0, >= 2.0.8)
      rack-test (>= 0.6.3)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.0, >= 1.2.0)
    actiontext (6.0.3.4)
      actionpack (= 6.0.3.4)
      activerecord (= 6.0.3.4)
      activestorage (= 6.0.3.4)
      activesupport (= 6.0.3.4)
      nokogiri (>= 1.8.5)
    actionview (6.0.3.4)
      activesupport (= 6.0.3.4)
      builder (~> 3.1)
      erubi (~> 1.4)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.1, >= 1.2.0)
    activejob (6.0.3.4)
      activesupport (= 6.0.3.4)
      globalid (>= 0.3.6)
    activemodel (6.0.3.4)
      activesupport (= 6.0.3.4)
    activerecord (6.0.3.4)
      activemodel (= 6.0.3.4)
      activesupport (= 6.0.3.4)
    activestorage (6.0.3.4)
      actionpack (= 6.0.3.4)
      activejob (= 6.0.3.4)
      activerecord (= 6.0.3.4)
      marcel (~> 0.3.1)
    activesupport (6.0.3.4)
      concurrent-ruby (~> 1.0, >= 1.0.2)
      i18n (>= 0.7, < 2)
      minitest (~> 5.1)
      tzinfo (~> 1.1)
      zeitwerk (~> 2.2, >= 2.2.2)
    addressable (2.7.0)
      public_suffix (>= 2.0.2, < 5.0)
    autoprefixer-rails (10.0.1.0)
      execjs
    bindex (0.8.1)
    bootsnap (1.4.8)
      msgpack (~> 1.0)
    bootstrap (4.5.2)
      autoprefixer-rails (>= 9.1.0)
      popper_js (>= 1.14.3, < 2)
      sassc-rails (>= 2.0.0)
    builder (3.2.4)
    byebug (11.1.3)
    capybara (3.33.0)
      addressable
      mini_mime (>= 0.1.3)
      nokogiri (~> 1.8)
      rack (>= 1.6.0)
      rack-test (>= 0.6.3)
      regexp_parser (~> 1.5)
      xpath (~> 3.2)
    childprocess (3.0.0)
    concurrent-ruby (1.1.7)
    crass (1.0.6)
    diff-lcs (1.4.4)
    enum_help (0.0.17)
      activesupport (>= 3.0.0)
    erubi (1.9.0)
    execjs (2.7.0)
    ffi (1.13.1)
    globalid (0.4.2)
      activesupport (>= 4.2.0)
    hpricot (0.8.6)
    html2slim (0.2.0)
      hpricot
    i18n (1.8.5)
      concurrent-ruby (~> 1.0)
    jbuilder (2.10.1)
      activesupport (>= 5.0.0)
    listen (3.1.5)
      rb-fsevent (~> 0.9, >= 0.9.4)
      rb-inotify (~> 0.9, >= 0.9.7)
      ruby_dep (~> 1.2)
    loofah (2.7.0)
      crass (~> 1.0.2)
      nokogiri (>= 1.5.9)
    mail (2.7.1)
      mini_mime (>= 0.1.1)
    marcel (0.3.3)
      mimemagic (~> 0.3.2)
    method_source (1.0.0)
    mimemagic (0.3.5)
    mini_mime (1.0.2)
    mini_portile2 (2.4.0)
    minitest (5.14.1)
    msgpack (1.3.3)
    nio4r (2.5.4)
    nokogiri (1.10.10)
      mini_portile2 (~> 2.4.0)
    pg (1.2.3)
    popper_js (1.16.0)
    public_suffix (4.0.5)
    puma (4.3.6)
      nio4r (~> 2.0)
    rack (2.2.3)
    rack-proxy (0.6.5)
      rack
    rack-test (1.1.0)
      rack (>= 1.0, < 3)
    rails (6.0.3.4)
      actioncable (= 6.0.3.4)
      actionmailbox (= 6.0.3.4)
      actionmailer (= 6.0.3.4)
      actionpack (= 6.0.3.4)
      actiontext (= 6.0.3.4)
      actionview (= 6.0.3.4)
      activejob (= 6.0.3.4)
      activemodel (= 6.0.3.4)
      activerecord (= 6.0.3.4)
      activestorage (= 6.0.3.4)
      activesupport (= 6.0.3.4)
      bundler (>= 1.3.0)
      railties (= 6.0.3.4)
      sprockets-rails (>= 2.0.0)
    rails-dom-testing (2.0.3)
      activesupport (>= 4.2.0)
      nokogiri (>= 1.6)
    rails-html-sanitizer (1.3.0)
      loofah (~> 2.3)
    railties (6.0.3.4)
      actionpack (= 6.0.3.4)
      activesupport (= 6.0.3.4)
      method_source
      rake (>= 0.8.7)
      thor (>= 0.20.3, < 2.0)
    rake (13.0.1)
    rb-fsevent (0.10.4)
    rb-inotify (0.10.1)
      ffi (~> 1.0)
    regexp_parser (1.8.1)
    rspec-core (3.9.3)
      rspec-support (~> 3.9.3)
    rspec-expectations (3.9.2)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.9.0)
    rspec-mocks (3.9.1)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.9.0)
    rspec-rails (3.9.1)
      actionpack (>= 3.0)
      activesupport (>= 3.0)
      railties (>= 3.0)
      rspec-core (~> 3.9.0)
      rspec-expectations (~> 3.9.0)
      rspec-mocks (~> 3.9.0)
      rspec-support (~> 3.9.0)
    rspec-support (3.9.3)
    ruby_dep (1.5.0)
    rubyzip (2.3.0)
    sass-rails (6.0.0)
      sassc-rails (~> 2.1, >= 2.1.1)
    sassc (2.4.0)
      ffi (~> 1.9)
    sassc-rails (2.1.2)
      railties (>= 4.0.0)
      sassc (>= 2.0)
      sprockets (> 3.0)
      sprockets-rails
      tilt
    selenium-webdriver (3.142.7)
      childprocess (>= 0.5, < 4.0)
      rubyzip (>= 1.2.2)
    slim (4.1.0)
      temple (>= 0.7.6, < 0.9)
      tilt (>= 2.0.6, < 2.1)
    slim-rails (3.2.0)
      actionpack (>= 3.1)
      railties (>= 3.1)
      slim (>= 3.0, < 5.0)
    spring (2.1.1)
    spring-watcher-listen (2.0.1)
      listen (>= 2.7, < 4.0)
      spring (>= 1.2, < 3.0)
    sprockets (4.0.2)
      concurrent-ruby (~> 1.0)
      rack (> 1, < 3)
    sprockets-rails (3.2.2)
      actionpack (>= 4.0)
      activesupport (>= 4.0)
      sprockets (>= 3.0.0)
    temple (0.8.2)
    thor (1.0.1)
    thread_safe (0.3.6)
    tilt (2.0.10)
    turbolinks (5.2.1)
      turbolinks-source (~> 5.2)
    turbolinks-source (5.2.0)
    tzinfo (1.2.7)
      thread_safe (~> 0.1)
    web-console (4.0.4)
      actionview (>= 6.0.0)
      activemodel (>= 6.0.0)
      bindex (>= 0.4.0)
      railties (>= 6.0.0)
    webdrivers (4.4.1)
      nokogiri (~> 1.6)
      rubyzip (>= 1.3.0)
      selenium-webdriver (>= 3.0, < 4.0)
    webpacker (4.3.0)
      activesupport (>= 4.2)
      rack-proxy (>= 0.6.1)
      railties (>= 4.2)
    websocket-driver (0.7.3)
      websocket-extensions (>= 0.1.0)
    websocket-extensions (0.1.5)
    xpath (3.2.0)
      nokogiri (~> 1.8)
    zeitwerk (2.4.0)

PLATFORMS
  ruby

DEPENDENCIES
  bootsnap (>= 1.4.2)
  bootstrap (= 4.5.2)
  byebug
  capybara (>= 2.15)
  enum_help (= 0.0.17)
  html2slim (= 0.2.0)
  jbuilder (~> 2.7)
  listen (>= 3.0.5, < 3.2)
  pg (>= 0.18, < 2.0)
  puma (~> 4.1)
  rails (~> 6.0.1)
  rspec-rails (< 4.0.0)
  sass-rails (>= 6)
  selenium-webdriver
  slim-rails (= 3.2.0)
  spring
  spring-watcher-listen (~> 2.0.0)
  turbolinks (~> 5)
  tzinfo-data
  web-console (>= 3.3.0)
  webdrivers
  webpacker (~> 4.0)

RUBY VERSION
   ruby 2.6.5p114

BUNDLED WITH
   2.1.4
4
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
4
1