LoginSignup
3
1

More than 1 year has passed since last update.

【RSpec】NameError: uninitialized constant FactoryBotというエラーが出た話

Last updated at Posted at 2020-10-28

こんにちは、あおい(https://twitter.com/aoi_engineer )です
今回は、RSpecにてテストコードを書いているときにNameError: uninitialized constant FactoryBotというエラーが出てしまった話をしていきたいと思います。

環境

rails:6.0.3.4

エラー内容

FactoryBotを導入後に

% bin/rspec      

を実行すると以下のようなエラーが出る。

An error occurred while loading ./spec/models/user_spec.rb.
Failure/Error: config.include FactoryBot::Syntax::Methods

NameError:
  uninitialized constant FactoryBot

原因

おそらくRSpecのFactoryBotがうまく反映されていない。

解決策

もう一度Gemfileを確認する。

group :development do
 #省略
 gem 'factory_bot_rails'
end

あ、、、記述する箇所を間違えていた。
正解は以下の「test」という記述のあるdevelopmentでしたね。

group :development, :test do
 #省略
 gem 'factory_bot_rails'
end

最後に

かなり初歩的なエラーでしたが、ハマる人は多いと思うので、ぜひ参考にしてください。

3
1
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
3
1