the industrial

ブログと言うより自分のためのメモ以外の何モノでもないです。でも読んでくださってありがとうございます。

FactoryBotが「Failure/Error: config.include FactoryBot::Syntax::Methods」で読み込めなかった話

Rails4からRails5にアップデートをする際、重要ロジックのテストを書いていないことに気づいた。

そこで、改めてRSpecを使おうと思いいろいろ調べている中で、テストデータ作成ツールに FactoryBot を利用することに。

しかし、いくら試せど下記のようなエラーが・・・。

Failure/Error: config.include FactoryBot::Syntax::Methods

NameError:
  uninitialized constant FactoryBot
# ./spec/rails_helper.rb:63:in `block in <top (required)>'
# ./spec/rails_helper.rb:33:in `<top (required)>'
# ./spec/services/xxxxxxs/xxxxxx_service_spec.rb:1:in `require'
# ./spec/services/xxxxxxs/xxxxxx_service_spec.rb:1:in `<top (required)>'
No examples found.


Finished in 0.00052 seconds (files took 12.32 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

原因は、Gemfileの定義で下記に様にしていたから笑

group :development do
  # テスト関連
  gem "rspec-rails"        # rspec本体
  gem "factory_bot_rails" # テストデータ作成
end

rails_helper.rbでは

ENV['RAILS_ENV'] ||= 'test'

としているため

group :test do
  # テスト関連
  gem "rspec-rails"        # rspec本体
  gem "factory_bot_rails" # テストデータ作成
end

が正解。

しょぼいことでハマって疲れた笑