thoughtbot / factory_bot_rails

Factory Bot ♥ Rails

Home Page:https://thoughtbot.com/services/ruby-on-rails

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Upgrading to 5.0.0 breaks all RSpec tests

colinwarmstrong opened this issue · comments

After upgrading to 5.0.0, I receive the following error for all of my RSpec spec files:

An error occurred while loading ./spec/*/*_spec.rb.
Failure/Error: require File.expand_path('../../config/environment', __FILE__)

RuntimeError:
  can't modify frozen Array
# ./config/initializers/apartment.rb:102:in `<top (required)>'
# ./config/environment.rb:5:in `<top (required)>'
# ./spec/rails_helper.rb:4:in `<top (required)>'

Downgrading solves the issue, not sure what exactly is going on.

That may be related to #303.
Do you get a different error if you run just one test at a time?

Same here. I (finally) did the upgrade process from factory_girl to factory_bot, it upgraded factory_bot to 5.0.0 and I have the same issue. I'm trying downgrading it a bit

@kofronpi if you are coming from factory_girl I would recommend upgrading to factory_bot 4.11.1 first so you can catch any deprecation warnings.

OK, if you do plan on upgrading later, my experience with that Frozen Array issue is that if you run just a single test you will get the "real" error message. I once traced through why we end up with a Frozen Array error instead of the underlying factory_bot error, if that helps at all: #303 (comment)

Ended up going back this morning to try and figure out the issue when upgrading.

So I ran an individual test like you suggested and got the "real" error message I was looking for:

NoMethodError:
  undefined method 'name' in 'award' factory

Apparently in the newest version of FactoryBot, factory attribute values must be defined using bracket notation like so:

FactoryBot.define do
  factory :award do
    name { 'Teamwork' }
  end
end

Without the brackets, you get an undefined method error for your attribute names. When you get multiple errors of this kind happening, it triggers a Frozen Array error instead.

So using the curly-bracket notation in all of my factories allowed me to run all my tests again without issues.


Also I was just reading through some of the FactoryBot docs and came across this:

Static attributes, without a block, are deprecated and will be removed in factory_bot 5.

So it makes sense that I was getting an error.

Yup, static attributes were deprecated in 4.11 and then removed in 5.0 (you can read this blog post for more details on why). The deprecation warning in 4.11 mentions a rubocop-rspec cop that can help change all the static attributes to dynamic ones. Let me know if you run into any other problems!