LoginSignup
13
10

More than 5 years have passed since last update.

RSpec3でテスト対象のクラスがuninitialized constantになる場合の対策

Posted at

RSpec3でテスト環境を再セットアップしていざ実行しようとしたら、対象のControllerやModelがuninitialized constantになって、「いや〜、規約どおりにやってるんだよな」と困っていた所、例によって、ちゃんとWiki読めやっていう話でした。。。

rspec/rspec-rails

上記にもある通り、Rspec3時代は、基本的にnot require "spec_helper" but require "rails_helper"とするようです。このような意図として、

  • Railsが必要がないテストに対しては、Railsをロードしなくてよい方法を提供する

があるようです。

/Users/yuichi/work/xxx/spec/controllers/hoge_controller_spec.rb:3:in `<top (required)>': uninitialized constant HogeController (NameError)

対象のClassのspecファイルの先頭を上記のrequire "spec_helper"にしたらうまくいきました。

参考URL

13
10
1

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
13
10