LoginSignup
5
5

More than 5 years have passed since last update.

Rspecでwarning: toplevel constantが出る現象に対応する

Last updated at Posted at 2016-02-10

注意

下記の記事は暫定的な対応にしかならず、本質的な対応方針ではないことがわかりました。
詳しくはコメント欄のやり取りと、
こちらの記事を参照ください。

現象

railsの開発で、名前空間が別で同じ名称のコントローラを作ることがあると思います。
例えば、hoges_controller.rbとfuga/hoges_controller.rbのような場合です。

ここでそれぞれにcontrollersのspecを書いて、

rake spec

等で一気に全部テストを実行すると、

warning: toplevel constant HogesController referenced by Fuga::HogesController

というような警告が出て、クラスがうまく読まれず、単発では通るspecが場合によってはエラーで落ちてしまうこともあります。

環境

  • ruby 2.2.1
  • rails 4.2.3
  • rspec 3.4.1

対応方法

下記のように設定を追加するとクラスが正しく読み込まれ、解決します。

spec/spec_helper.rb
Dir[File.expand_path("app/controllers/fuga/*.rb")].each do |file|
  require file
end
5
5
6

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
5
5