LoginSignup
29
10

More than 5 years have passed since last update.

Factory_Botで undefined method 'name' in 'user' factoryが発生する場合

Last updated at Posted at 2019-03-10

RailsでFactory Botを使う時にタイトルのエラーが発生したので、原因と対応のメモです。

バージョン

Ruby 2.5.1
Rails 5.2.2
Factory Bot 5.0.1

エラーメッセージ

具体的には以下のエラーメッセージが出力されました。

/vendor/bundle/ruby/2.5.0/gems/factory_bot-5.0.1/lib/factory_bot
/definition_proxy.rb:97:in `method_missing': 
undefined method 'name' in 'user' factory (NoMethodError)

解決方法

エラー発生時のファクトリ

spec/factories/user.rb
FactoryBot.define do
  factory :user do
    name "Beer Lover"
    email "beerlover@example.com"
    password "beerlover"
  end
end

↓修正版

spec/factories/user.rb
FactoryBot.define do
  factory :user do
    name {"Beer Lover"}
    email {"beerlover@example.com"}
    password {"beerlover"}
  end
end

属性名に設定する値は{}で囲う必要があります。
Factory Botのバージョンが5.0.0からは{}で囲わないとエラーがでるように変更されたみたいですね。

29
10
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
29
10