LoginSignup
0
1

More than 3 years have passed since last update.

RspecでFailure/Error: require File.expand_path('../config/environment', __dir__)の原因と対処

Posted at

はじめに

Rspecを導入した時に以下のエラーと遭遇しました。

An error occurred while loading ./spec/models/user_spec.rb.
Failure/Error: require File.expand_path('../config/environment', __dir__)

NoMethodError:
  undefined method `active_storage' for #<Rails::Application::Configuration:0x000055b12abba7c0>

Rails: 6.0.3.4
rspec-rails: 4.0.1


対象となる読者

Rspec導入したけど動かないよ。タイトルのエラー出てるよ。という人向け。


結論

原因はactive_storageのroutingを出したくないので、 config/application.rb

application.rb
# require "active_storage/engine"

上記記述をコメントアウトしていたことでした。

application.rb
require "active_storage/engine"

この状態に戻すとテストが通った。

やったね!

以下の記事のAnswerにあるように、require "active_storage/engine"の記述は他のフレームワークと使う時に必要なようです。
'method_missing': undefined method `active_storage' for (NoMethodError)

類似のパターンとしてはbundle updateのタイミングで発生することがあるようです。

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