Search code examples
ruby-on-railsruby-on-rails-5factory-bot

Factorybot doesn't recognize any attributes—"method missing" errors


I recently upgraded a bunch of gems, and since then I can't run rails server, console, or rspec.

Here's the error log I see:

NoMethodError:
  undefined method 'name' in 'book' factory
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/definition_proxy.rb:97:in `method_missing'
# ./spec/factories/books.rb:3:in `block (2 levels) in <top (required)>'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/syntax/default.rb:18:in `instance_eval'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/syntax/default.rb:18:in `factory'
# ./spec/factories/books.rb:2:in `block in <top (required)>'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/syntax/default.rb:49:in `instance_eval'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/syntax/default.rb:49:in `run'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/syntax/default.rb:7:in `define'
# ./spec/factories/books.rb:1:in `<top (required)>'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/find_definitions.rb:20:in `block (2 levels) in find_definitions'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/find_definitions.rb:19:in `each'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/find_definitions.rb:19:in `block in find_definitions'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/find_definitions.rb:15:in `each'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/find_definitions.rb:15:in `find_definitions'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.0.2/lib/factory_bot/reload.rb:6:in `reload'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot_rails-5.0.2/lib/factory_bot_rails/reloader.rb:25:in `block in build_reloader'
# /Users/regan/.rvm/gems/ruby-2.6.3/gems/factory_bot_rails-5.0.2/lib/factory_bot_rails/reloader.rb:35:in `block in register_reloader'
# ./config/environment.rb:5:in `<top (required)>'
# ./spec/rails_helper.rb:3:in `require'
# ./spec/rails_helper.rb:3:in `<top (required)>'
# ./spec/controllers/api/v1/songs_controller_spec.rb:1:in `require'
# ./spec/controllers/api/v1/songs_controller_spec.rb:1:in `<top (required)>'

Part of the upgrade included upgrading rails to 5.2.3, which forced me to remove this line from new_framework_defaults.rb file:

# Do not halt callback chains when a callback returns false. Previous versions had true.
ActiveSupport.halt_callback_chains_on_return_false = false

I'm guessing this has something to do with it?


Edit: The factory code is below. This exact code was working 100% before upgrading the gems:

FactoryBot.define do
  factory :book do
    name "my songbook"
  end
end

Solution

  • FactoryBot changed its syntax. It now expects a block when assigning values – even static values – to variables. Change your code to this:

    FactoryBot.define do
      factory :book do
        name { "my songbook" }
      end
    end
    

    See Defining Factories in their docs.