factory_bot_rails: factory_girl_rails/factory_bot_rails RuntimeError: can't modify frozen Array
I’ve followed the instructions in the blog post about how to make the factory_girl_rails/factory_bot_rails rename and now tests fail with ‘can’t modify frozen Array’, eg:
RuntimeError:
can't modify frozen Array
# ./config/environment.rb:5:in `<top (required)>'
# ./spec/rails_helper.rb:2:in `<top (required)>'
# ./spec/models/user_state_machine_spec.rb:1:in `<top (required)>'
I’m totally new to rails and Ruby so likely a user error, but what? Any pointers on what I’m doing wrong?
Thanks!
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 4
- Comments: 29 (7 by maintainers)
Great tip @composerinteralia.
basically to solve this issue:
The errors with 5.0 occur because of the deprecation of static var in factories. Gl all.
Having the same problem after changing to FactoryBot from FactoryGirl and
factory_girl_rails
tofactory_bot_rails
NOTE: if you get this test error, the very first test error will give you more details on what’s breaking.
In my case, just changed my factory field from
password 123456
topassword { 123456 }
solved.@nezirz there were some deprecations in factory_bot 4.11, so you will want to upgrade to that version and deal with the warnings before upgrading to factory_bot 5.0.
gem install rubocop-rspec
and thenrubocop --require rubocop-rspec --only FactoryBot/AttributeDefinedStatically --auto-correct
worked for me
@joshuaclayton nothing too useful, I’m afraid.
Here it is, in case it helps:
This is on Rails 5.2.1 and Ruby 2.5.1
(Edits: Bad copy and paste when removing long [gem path] 👍)
I had the same problem when tried to upgrade from
rails 5.2.0 to 5.2.2.1
using bundle update rails command, its also updated many other gems one of them was factory_bot from 4.10.0 to 5.0.2 so solution was to downgrade to 4.10.0 by specifying the version number for factory_bot togem "factory_bot_rails", "~> 4.10.0"
This happened to me when I declared two factories w/ the same name.
@joshuaclayton - Fast response! There was a few, but we’ve since narrowed it down to this gem being bumped - eg; we took this upgrade out and everything was fine.
(Edit: tag)
This same issue seems to have happened to us on the update from 4.10.0 to 4.11.0, although the releases show “No notable changes”
We literally changed the version number and bundled.
@schmijos there are no intentional breaking changes from factory_bot_rails 5.0.1 to 5.0.2. The FrozenError is not coming from factory_bot (see https://github.com/thoughtbot/factory_bot_rails/issues/303 for details). If you want to see the underlying factory_bot error try running just one of the failing specs at a time.
Sorted! The problem was that the grep command given in the blog post to Replace All Constant References didn’t actually change all the instances of FactoryGirl in the code base. After we found that and did a global change with Atom it all works fine. This is an example diff of one of the files missed by the grep that I had to manually change:
Something like this grep looks like it fixes it:
grep -rl 'FactoryGirl' ./ | xargs sed -i '' 's/FactoryGirl/FactoryBot/g'
I was getting the same error. I fixed by removing require ‘rails_helper’ from spec files. It was causing the issue for me. I am not sure but it seems somewhere it was trying to load these helpers multiple time and it was causing can’t modify frozen Array.
The stack trace given on rails/rails#33745 looks fairly similar to this, and it seems to have been related to a rails_admin initializer running after the
build_middleware_stack
initializer.The
build_middleware_stack
initializer freezes the middleware array:From the stack trace in this issue, it looks like when sentry-raven inserts its middleware it is happening after the middleware array has been frozen.
I don’t see how factory_bot_rails could have much effect here, since we don’t do anything with middleware at all, but we did just got a new issue about this: #303.
In my case issue was caused by rails_admin recent upgrade: