8
3

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 3 years have passed since last update.

RSpec導入時のエラー[NameError: uninitialized constant FactoryBot]

Last updated at Posted at 2020-09-08

エラー内容

某スクールでフリマアプリの開発中。RSpecを用いてテストを行った際のエラーです。

ターミナルでbundle exec rspec spec/models/user_spec.rbを行ったところエラーが発生しました。

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

NameError:
  uninitialized constant FactoryBot
# ./spec/rails_helper.rb:35:in `block in <top (required)>'
# ./spec/rails_helper.rb:32:in `<top (required)>'
# ./spec/models/user_spec.rb:1:in `require'
# ./spec/models/user_spec.rb:1:in `<top (required)>'
# ------------------
# --- Caused by: ---
# NameError:
#   uninitialized constant FactoryBot
#   ./spec/rails_helper.rb:35:in `block in <top (required)>'
No examples found.

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

行ったこと

gem'rspec-rails'gem 'factory_bot_rails'をGemfileに追記し、ターミナルで bundle install

group :development, :test do
  #中略
  gem 'rspec-rails'
  gem 'factory_bot_rails
end

rails g rspec:installでファイルを生成

#ターミナル
      create  .rspec
      create  spec
      create  spec/spec_helper.rb
      create  spec/rails_helper.rb

③ .rspecに以下を追加

--format documentation

④ spec/modelsディレクトリを追加し、その中にmodelsディテクトリ下に「user_spec.rb」をという名前で作成し、テストコードを記述

describe User do
  describe '#create' do

   it "必須項目が入力してあれば登録できること" do
      user = build(:user)
      expect(user).to be_valid
    end

#省略

⑤ spec/factoriesディレクトリを追加し、その中に「users.rb」という名前でファイルを生成し、インスタンスを作成

FactoryBot.define do
  factory :user do
    nickname              {"yamada"}
    email                 {"yamada@gmail.com"}
    password              {"00000000"}
    password_confirmation {"00000000"}
    familyname_kanji      {"山田"}
    firstname_kanji       {"太郎"}
    familyname_kana       {"ヤマダ"}
    firstname_kana        {"タロウ"}
    birthday              {"2020-01-01"}
  end
end

⑥factory_botの記法の省略のため、spec/rails_helper.rbを以下のように編集

#省略
RSpec.configure do |config|
  #下記の記述を追加
  config.include FactoryBot::Syntax::Methods
  #省略
end

⑦ テストコードを確認するために、bundle exec rspec spec/models/user_spec.rb を実行したところエラーが発生

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

NameError:
  uninitialized constant FactoryBot
# ./spec/rails_helper.rb:35:in `block in <top (required)>'
# ./spec/rails_helper.rb:32:in `<top (required)>'
# ./spec/models/user_spec.rb:1:in `require'
# ./spec/models/user_spec.rb:1:in `<top (required)>'
# ------------------
# --- Caused by: ---
# NameError:
#   uninitialized constant FactoryBot
#   ./spec/rails_helper.rb:35:in `block in <top (required)>'
No examples found.

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

RSpecを導入した時、詰まったこと(NameError: uninitialized constant FactoryBot)を参考にconfig.include FactoryBot::Syntax::Methodsの配置を変えてみたが、解決できず…

エラーの原因

factory_bot_railsのバージョンが原因。バージョンが違ったので読み込まれず、エラーが発生していたようです。

gem 'factory_bot_rails', '5.2.0'

さいごに

初歩的なミスでしたが、前例が見つからず手間取ったので、同じエラーに遭遇した方の参考になれば幸いです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?