質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

1回答

1404閲覧

Rails RspecでFactoryBotを使おうとすると、NameErrorになる

mk_4423

総合スコア6

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

1クリップ

投稿2019/12/06 06:03

###前提・試したいこと
Railsを学習し始めてから3ヶ月ほどの初心者です。

RSpecにて、FactoryBotを用いて、ユーザーの作成をしたいのですが、NameErrorになります。

###発生している問題・エラーメッセージ

Failure/Error: config.include FactoryBot::Syntax::Methods NameError: uninitialized constant FactoryBot # /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:79:in `block in load_missing_constant' # /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:8:in `without_bootsnap_cache' # /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:79:in `rescue in load_missing_constant' # /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:58:in `load_missing_constant' # ./spec/rails_helper.rb:63:in `block in <top (required)>' # ./spec/rails_helper.rb:35:in `<top (required)>' # ./spec/requests/worker_users_spec.rb:1:in `<top (required)>' # ------------------ # --- Caused by: --- # NameError: # uninitialized constant FactoryBot # /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:60:in `block in load_missing_constant' No examples found. Finished in 0.00012 seconds (files took 7.56 seconds to load) 0 examples, 0 failures, 1 error occurred outside of examples

###該当のソースコード
requests/worker_user_spec.rb

require 'rails_helper' RSpec.describe "WorkerUsers", type: :request do describe "ログインページにアクセスすると" do context '未ログインの場合' do it "リクエストが成功すること。" do get new_worker_user_session_path expect(response).to have_http_status(200) end end context 'ログイン済みの場合' do before do user = FactoryBot.create(:worker_user) sign_in user end it "ユーザーページにリダイレクトされること。" do get new_worker_user_session_path expect(response).to have_http_status(302) end end end # describe "会員登録ページにアクセスすると" do # it "リクエストが成功すること。" do # get new_worker_user_registration_path # expect(response).to have_http_status(200) # end # end end

factorys/worker_users.rb

# frozen_string_literal: true FactoryBot.define do factory :worker_user do sei { 'サンプル'} mei { '太郎'} sequence(:email) { |n| "sample#{n}@example.com" } password { 'pasword' } confirmed_at { Date.today } end end

spec/rails_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install' require 'spec_helper' ENV['RAILS_ENV'] ||= 'test' require File.expand_path('../config/environment', __dir__) # Prevent database truncation if the environment is production abort("The Rails environment is running in production mode!") if Rails.env.production? require 'rspec/rails' # Add additional requires below this line. Rails is not loaded until this point! # Requires supporting ruby files with custom matchers and macros, etc, in # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are # run as spec files by default. This means that files in spec/support that end # in _spec.rb will both be required and run as specs, causing the specs to be # run twice. It is recommended that you do not name files matching this glob to # end with _spec.rb. You can configure this pattern with the --pattern # option on the command line or in ~/.rspec, .rspec or `.rspec-local`. # # The following line is provided for convenience purposes. It has the downside # of increasing the boot-up time by auto-requiring all files in the support # directory. Alternatively, in the individual `*_spec.rb` files, manually # require only the support files necessary. # Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f } # Checks for pending migrations and applies them before tests are run. # If you are not using ActiveRecord, you can remove these lines. begin ActiveRecord::Migration.maintain_test_schema! rescue ActiveRecord::PendingMigrationError => e puts e.to_s.strip exit 1 end RSpec.configure do |config| # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures config.fixture_path = "#{::Rails.root}/spec/fixtures" # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, remove the following line or assign false # instead of true. config.use_transactional_fixtures = true # RSpec Rails can automatically mix in different behaviours to your tests # based on their file location, for example enabling you to call `get` and # `post` in specs under `spec/controllers`. # # You can disable this behaviour by removing the line below, and instead # explicitly tag your specs with their type, e.g.: # # RSpec.describe UsersController, :type => :controller do # # ... # end # # The different available types are documented in the features, such as in # https://relishapp.com/rspec/rspec-rails/docs config.infer_spec_type_from_file_location! config.include RequestSpecHelper, type: :request config.include Devise::Test::IntegrationHelpers, type: :system # Filter lines from Rails gems in backtraces. config.filter_rails_from_backtrace! # arbitrary gems may also be filtered via: # config.filter_gems_from_backtrace("gem name") end

Gemfile

# frozen_string_literal: true source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '2.6.3' gem 'bootsnap', '>= 1.1.0', require: false gem 'coffee-rails', '~> 4.2' gem 'jbuilder', '~> 2.5' gem 'pg', '>= 0.18', '< 2.0' gem 'puma', '~> 3.11' gem 'rails', '~> 5.2.3' gem 'sass-rails', '~> 5.0' gem 'turbolinks', '~> 5' gem 'uglifier', '>= 1.3.0' group :development, :test do gem 'byebug', platforms: %i[mri mingw x64_mingw] end group :development do gem 'factory_bot_rails', '~> 5.1' gem 'listen', '>= 3.0.5', '< 3.2' gem 'rspec-rails' gem 'rubocop', '~> 0.76.0' gem 'rubocop-performance', '~> 1.5' gem 'spring', '~> 2.1' gem 'spring-watcher-listen', '~> 2.0.0' gem 'web-console', '>= 3.3.0' end group :test do gem 'capybara', '>= 2.15' gem 'chromedriver-helper' gem 'selenium-webdriver' end gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby] gem 'acts-as-taggable-on', '~> 6.0' gem 'carrierwave', '1.2.2' gem 'devise' gem 'faker' gem 'html2slim' gem 'i18n', '~> 1.7' gem 'mini_magick', '4.7.0' gem 'omniauth-facebook' gem 'ransack', '~> 2.3' gem 'slim-rails', '3.1.3' gem 'kaminari', '>= 1.1.1'

###試したこと
コンソール似て、FactoryBot.createを実行してみました。

irb(main):001:0> FactoryBot.create(:worker_user)

すると、正常にユーザーを作成することができました。

###補足情報(FW/ツールのバージョンなど)
ruby 2.6.2
rails 5.2.3
macOS 10.14.6

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

FactoryBotが認識されていないですね。
spec/rails_helper.rb に FactroyBotの定義はしましたか?

投稿2019/12/12 11:12

winterboum

総合スコア23349

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問