factory_girl_rails/factory_bot_rails RuntimeError: can't modify frozen Array - Gherkin factory_bot_rails

I've followed the instructions in the blog post about how to make the factorygirlrails/factorybotrails 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!

Asked Nov 07 '21 14:11
avatar torntrousers
torntrousers

9 Answer:

Having the same problem after changing to FactoryBot from FactoryGirl and factory_girl_rails to factory_bot_rails

NOTE: if you get this test error, the very first test error will give you more details on what's breaking.

1
Answered Sep 05 '18 at 00:08
avatar  of KelseyDH
KelseyDH

In my case, just changed my factory field from password 123456 to password { 123456 } solved.

1
Answered May 25 '19 at 01:17
avatar  of marcelobarreto
marcelobarreto

@nezirz there were some deprecations in factorybot 4.11, so you will want to upgrade to that version and deal with the warnings before upgrading to factorybot 5.0.

1
Answered Mar 13 '19 at 21:58
avatar  of composerinteralia
composerinteralia

gem install rubocop-rspec and then rubocop --require rubocop-rspec --only FactoryBot/AttributeDefinedStatically --auto-correct

worked for me

1
Answered Oct 02 '19 at 16:46
avatar  of mike927
mike927

@joshuaclayton nothing too useful, I'm afraid.

Here it is, in case it helps:

An error occurred while loading .<spec_path>_spec.rb.
Failure/Error: require File.expand_path("../../config/environment", __FILE__)

FrozenError:
  can't modify frozen Array
# [gem path]/actionpack-5.2.1/lib/action_dispatch/middleware/stack.rb:76:in `insert'
# [gem path]/actionpack-5.2.1/lib/action_dispatch/middleware/stack.rb:76:in `insert'
# [gem path]/sentry-raven-2.7.4/lib/raven/integrations/rails.rb:10:in `block in <class:Rails>'
# [gem path]/railties-5.2.1/lib/rails/initializable.rb:32:in `instance_exec'
# [gem path]/railties-5.2.1/lib/rails/initializable.rb:32:in `run'
# [gem path]/railties-5.2.1/lib/rails/initializable.rb:61:in `block in run_initializers'
# [gem path]/railties-5.2.1/lib/rails/initializable.rb:60:in `run_initializers'
# [gem path]/railties-5.2.1/lib/rails/application.rb:361:in `initialize!'
# ./config/environment.rb:17:in `<top (required)>'
# [gem path]/activesupport-5.2.1/lib/active_support/dependencies.rb:287:in `require'
# [gem path]/activesupport-5.2.1/lib/active_support/dependencies.rb:287:in `block in require'
# [gem path]/activesupport-5.2.1/lib/active_support/dependencies.rb:253:in `load_dependency'
# [gem path]/activesupport-5.2.1/lib/active_support/dependencies.rb:287:in `require'
# ./spec/spec_helper.rb:14:in `<top (required)>'
# [gem path]/activesupport-5.2.1/lib/active_support/dependencies.rb:287:in `require'
# [gem path]/activesupport-5.2.1/lib/active_support/dependencies.rb:287:in `block in require'
# [gem path]/activesupport-5.2.1/lib/active_support/dependencies.rb:253:in `load_dependency'
# [gem path]/activesupport-5.2.1/lib/active_support/dependencies.rb:287:in `require'
# .<spec_path>_spec.rb:1:in `<top (required)>'
# [gem path]/zeus-0.15.14/lib/zeus/load_tracking.rb:82:in `load'
# [gem path]/zeus-0.15.14/lib/zeus/load_tracking.rb:82:in `load'
# [gem path]/zeus-0.15.14/lib/zeus/load_tracking.rb:74:in `load'
# [gem path]/activesupport-5.2.1/lib/active_support/dependencies.rb:281:in `block in load'
# [gem path]/activesupport-5.2.1/lib/active_support/dependencies.rb:253:in `load_dependency'
# [gem path]/activesupport-5.2.1/lib/active_support/dependencies.rb:281:in `load'

This is on Rails 5.2.1 and Ruby 2.5.1

(Edits: Bad copy and paste when removing long [gem path] :+1:)

1
Answered Aug 29 '18 at 09:58
avatar  of JalisoCSP
JalisoCSP

I had the same problem when tried to upgrade fromrails 5.2.0 to 5.2.2.1using bundle update rails command, its also updated many other gems one of them was factorybot from 4.10.0 to 5.0.2 so solution was to downgrade to 4.10.0 by specifying the version number for factorybot to gem "factory_bot_rails", "~> 4.10.0"

1
Answered Mar 13 '19 at 21:43
avatar  of nezirz
nezirz

This happened to me when I declared two factories w/ the same name.

1
Answered Nov 06 '18 at 01:59
avatar  of lstone
lstone

@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.

Failure/Error: require File.expand_path("../../config/environment", __FILE__)

FrozenError:
  can't modify frozen Array

# ./config/environment.rb:17:in `<top (required)>'
# ./spec/spec_helper.rb:14:in `<top (required)>'
# ./spec/<our_spec_file>_spec.rb:1:in `<top (required)>'

respectively:
-> Rails.application.initialize!
-> require File.expand_path("../../config/environment", __FILE__)
-> require 'spec_helper'

(Edit: tag)

1
Answered Aug 28 '18 at 13:34
avatar  of JalisoCSP
JalisoCSP

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:

git diff spec/factories/activation_code.rb
diff --git a/spec/factories/activation_code.rb b/spec/factories/activation_code.rb
index 493f7942..8c727cb5 100644
--- a/spec/factories/activation_code.rb
+++ b/spec/factories/activation_code.rb
@@ -1,4 +1,4 @@
-FactoryGirl.define do
+FactoryBot.define do
   factory :activation_code do
     code { Faker::Lorem.characters(6) }
     code_valid { true }

Something like this grep looks like it fixes it: grep -rl 'FactoryGirl' ./ | xargs sed -i '' 's/FactoryGirl/FactoryBot/g'

1
Answered Oct 27 '17 at 09:24
avatar  of torntrousers
torntrousers