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

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

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

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

RSpec

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

Q&A

解決済

1回答

7148閲覧

RSpec+FactoryBotでエラー「KeyError:Factory not registered:」

pecchan

総合スコア555

Ruby on Rails 5

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

RSpec

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

0グッド

0クリップ

投稿2020/03/29 03:52

初めてrspecを勉強してます。
FactoryBotにて、userモデルのmodelテストを行うと以下のエラーになりました。

rails_user:~/environment/gv (master) $ bundle exec rspec spec/models/user_spec.rb F. Failures: 1) User 名前、メール、パスワードがある場合、有効であること Failure/Error: user = FactoryBot.build(:user) KeyError: Factory not registered: "user" # /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.1.1/lib/factory_bot/registry.rb:23:in `find' # /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.1.1/lib/factory_bot/decorator.rb:10:in `method_missing' # /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.1.1/lib/factory_bot/internal.rb:76:in `factory_by_name' # /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.1.1/lib/factory_bot/factory_runner.rb:12:in `run' # /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.1.1/lib/factory_bot/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method' # ./spec/models/user_spec.rb:5:in `block (2 levels) in <top (required)>' # ------------------ # --- Caused by: --- # KeyError: # key not found: "user" # /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.1.1/lib/factory_bot/registry.rb:23:in `find' Finished in 0.89045 seconds (files took 1.98 seconds to load) 1 examples, 1 failure Failed examples: rspec ./spec/models/user_spec.rb:4 # User 名前、メール、パスワードがある場合、有効であること

/gv/spec/models/user_spec.rb

ruby

1require 'rails_helper' 2 3RSpec.describe User, type: :model do 4 it "名前、メール、パスワードがある場合、有効であること" do 5 user = FactoryBot.build(:user) 6 expect(user).to be_valid 7 end 8end

/gv/spec/spec_helper.rb

ruby

1# This file was generated by the `rails generate rspec:install` command. Conventionally, all 2# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. 3# The generated `.rspec` file contains `--require spec_helper` which will cause 4# this file to always be loaded, without a need to explicitly require it in any 5# files. 6# 7# Given that it is always loaded, you are encouraged to keep this file as 8# light-weight as possible. Requiring heavyweight dependencies from this file 9# will add to the boot time of your test suite on EVERY test run, even for an 10# individual file that may not need all of that loaded. Instead, consider making 11# a separate helper file that requires the additional dependencies and performs 12# the additional setup, and require it from the spec files that actually need 13# it. 14# 15# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration 16require 'capybara/rspec' 17RSpec.configure do |config| 18 config.before(:all) do 19 FactoryBot.reload 20 end 21 config.before(:each, type: :system) do 22 driven_by :selenium_chrome_headless 23 end 24 # rspec-expectations config goes here. You can use an alternate 25 # assertion/expectation library such as wrong or the stdlib/minitest 26 # assertions if you prefer. 27 config.expect_with :rspec do |expectations| 28 # This option will default to `true` in RSpec 4. It makes the `description` 29 # and `failure_message` of custom matchers include text for helper methods 30 # defined using `chain`, e.g.: 31 # be_bigger_than(2).and_smaller_than(4).description 32 # # => "be bigger than 2 and smaller than 4" 33 # ...rather than: 34 # # => "be bigger than 2" 35 expectations.include_chain_clauses_in_custom_matcher_descriptions = true 36 end 37 # rspec-mocks config goes here. You can use an alternate test double 38 # library (such as bogus or mocha) by changing the `mock_with` option here. 39 config.mock_with :rspec do |mocks| 40 # Prevents you from mocking or stubbing a method that does not exist on 41 # a real object. This is generally recommended, and will default to 42 # `true` in RSpec 4. 43 mocks.verify_partial_doubles = true 44 end 45 # This option will default to `:apply_to_host_groups` in RSpec 4 (and will 46 # have no way to turn it off -- the option exists only for backwards 47 # compatibility in RSpec 3). It causes shared context metadata to be 48 # inherited by the metadata hash of host groups and examples, rather than 49 # triggering implicit auto-inclusion in groups with matching metadata. 50 config.shared_context_metadata_behavior = :apply_to_host_groups 51# The settings below are suggested to provide a good initial experience 52# with RSpec, but feel free to customize to your heart's content. 53=begin 54 # This allows you to limit a spec run to individual examples or groups 55 # you care about by tagging them with `:focus` metadata. When nothing 56 # is tagged with `:focus`, all examples get run. RSpec also provides 57 # aliases for `it`, `describe`, and `context` that include `:focus` 58 # metadata: `fit`, `fdescribe` and `fcontext`, respectively. 59 config.filter_run_when_matching :focus 60 # Allows RSpec to persist some state between runs in order to support 61 # the `--only-failures` and `--next-failure` CLI options. We recommend 62 # you configure your source control system to ignore this file. 63 config.example_status_persistence_file_path = "spec/examples.txt" 64 # Limits the available syntax to the non-monkey patched syntax that is 65 # recommended. For more details, see: 66 # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ 67 # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ 68 # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode 69 config.disable_monkey_patching! 70 # Many RSpec users commonly either run the entire suite or an individual 71 # file, and it's useful to allow more verbose output when running an 72 # individual spec file. 73 if config.files_to_run.one? 74 # Use the documentation formatter for detailed output, 75 # unless a formatter has already been configured 76 # (e.g. via a command-line flag). 77 config.default_formatter = "doc" 78 end 79 # Print the 10 slowest examples and example groups at the 80 # end of the spec run, to help surface which specs are running 81 # particularly slow. 82 config.profile_examples = 10 83 # Run specs in random order to surface order dependencies. If you find an 84 # order dependency and want to debug it, you can fix the order by providing 85 # the seed, which is printed after each run. 86 # --seed 1234 87 config.order = :random 88 # Seed global randomization in this process using the `--seed` CLI option. 89 # Setting this allows you to use `--seed` to deterministically reproduce 90 # test failures related to randomization by passing the same `--seed` value 91 # as the one that triggered the failure. 92 Kernel.srand config.seed 93=end 94end

/gv/Gemfile

ruby

1source 'https://rubygems.org' 2 3gem 'rails', '5.1.6' 4gem 'bootstrap-sass', '3.3.7' 5gem 'puma', '3.9.1' 6gem 'sass-rails', '5.0.6' 7gem 'uglifier', '3.2.0' 8gem 'coffee-rails', '4.2.2' 9gem 'jquery-rails', '4.3.1' 10gem 'turbolinks', '5.0.1' 11gem 'jbuilder', '2.7.0' 12gem 'bcrypt', '3.1.12' 13 14group :development, :test do 15 gem 'sqlite3', '1.3.13' 16 gem 'byebug', '9.0.6', platform: :mri 17 #gem 'spring-commands-rspec' 18end 19 20group :development do 21 gem 'web-console', '3.5.1' 22 gem 'listen', '3.1.5' 23 gem 'spring', '2.0.2' 24 gem 'spring-watcher-listen', '2.0.1' 25 #gem 'pry-rails' 26 #gem 'pry-byebug' 27 #gem 'pry-doc' 28end 29 30group :test do 31 gem 'rails-controller-testing', '1.0.2' 32 gem 'minitest-reporters', '1.1.14' 33 gem 'guard', '2.13.0' 34 gem 'guard-minitest', '2.4.4' 35 gem 'rspec-rails' 36 gem 'factory_bot_rails' 37 gem 'faker' 38 gem 'capybara', '~> 2.13' 39 #gem 'database_cleaner' 40 gem 'launchy' 41 gem 'selenium-webdriver' 42 gem 'webdrivers' 43end 44 45group :production do 46 gem 'pg', '0.20.0' 47end 48 49# Windows環境ではtzinfo-dataというgemを含める必要があります 50gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

/gv/app/models/user.rb

ruby

1class User < ApplicationRecord 2 has_secure_password 3 validates :name, presence: true 4end 5

エラーメッセージでググったところ、以下のような情報があり、試したのですが結果変わらずです。
spec/factories以下に正しいfactoryの定義が存在するのにFactory not registered

分かる方教えて下さい。

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

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

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

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

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

guest

回答1

0

自己解決

spec/factories/user.rb
を用意したらいけました!

投稿2020/03/29 05:58

pecchan

総合スコア555

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問