1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

uninitialized constant FactoryBotのエラーが出た時の話

Last updated at Posted at 2022-03-02

出たエラーは下記

1) User ユーザー新規登録 内容に問題ない場合 すべての値が正しく入力されていれば保存できること
     Failure/Error: @user = FactoryBot.build(:user)
     
     NameError:
       uninitialized constant FactoryBot
     # ./spec/models/user_spec.rb:6:in `block (3 levels) in <top (required)>'

確認箇所

・FactoryBotの記述内容
・user_spec.rbの記述内容

上記確認しても誤字は見当たらない

uninitialized constant
⇨翻訳すると”未初期化の定数”
ということは、、、

Gemfileを見ると下記のように記述していました。。。

Gemfile
group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
  gem 'rspec-rails'
  gem 'factory_bot_rails'
end

正しくはこう

Gemfile
group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'rspec-rails'
  gem 'factory_bot_rails'
end

bundle installコマンドを実行後、再度テストコード実行コマンドを叩き解決しました。
めちゃくちゃ凡ミスでしたが、いい勉強になりました。。。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?