Factory_bot: Rails 5 - Undefined Method for all factories

Created on 6 Mar 2019  路  6Comments  路  Source: thoughtbot/factory_bot

When using version 5.0.2 of the factory_bot_rails gem, my factories now all give me an Undefined Method error for all my registered factories. If I downgrade the version of the gem to 4.11 it works.

spec_helper.rb

RSpec.configure do |config|

  config.before(:suite) do
    FactoryBot.definition_file_paths = [File.expand_path('../factories', __FILE__)]
    FactoryBot.find_definitions
  end
...
end

I'm using Rails 5.2.

Any help would be appreciated.

Most helpful comment

I think a more explicit answer would have greatly helped.

From the blog by Thoughbot, I came to realize that statically defined attributes have been deprecated from factory_bot >= 5.0. And so rather than use statically defined attributes for your factories, use dynamically defined attributes

Here's an example

Use Dynamically defined attribues
factory :robot do
name { "Ralph" }
email { "[email protected]" }
end

And not statically defined attributes

factory :robot do
name "Ralph"
email "[email protected]"
end

You can read up more about it here
https://thoughtbot.com/blog/deprecating-static-attributes-in-factory_bot-4-11

All 6 comments

This sounds similar to https://github.com/thoughtbot/factory_bot_rails/issues/313. Any chance this is a result of static attributes being deprecated and then removed. Are you getting any deprecation warnings on 4.11?

If it is not that, can you provide a backtrace?

I do get the deprecation warnings on 4.11 yes

Got it. You will need to address those deprecation warnings before you can upgrade to factory_bot_rails 5. Let me know if you have any trouble with that.

I tried to improve the error message a bit in https://github.com/thoughtbot/factory_bot/pull/1287. Hopefully that will help make it more clear what is going on if somebody tries to update directly from factory_bot <= 4.10 straight to factory_bot >= 5.0 without first going to 4.11

I think a more explicit answer would have greatly helped.

From the blog by Thoughbot, I came to realize that statically defined attributes have been deprecated from factory_bot >= 5.0. And so rather than use statically defined attributes for your factories, use dynamically defined attributes

Here's an example

Use Dynamically defined attribues
factory :robot do
name { "Ralph" }
email { "[email protected]" }
end

And not statically defined attributes

factory :robot do
name "Ralph"
email "[email protected]"
end

You can read up more about it here
https://thoughtbot.com/blog/deprecating-static-attributes-in-factory_bot-4-11

Was this page helpful?
0 / 5 - 0 ratings