简体   繁体   中英

Error undefined constant and frozen Array during Rspec and FactoryBot

I'm running through an error during Rspec. When I run rspec the following errors happen.

I've seen some questions and answers here but none of them helped me. One of them included downgraded Factory Bot to 4.10 and run rubocop to modify the constants while upgrade to 5.0.

I have no idea what is going on. What am I missing here?

NoMethodError:

undefined method 'container_size_cd' in 'container_weight_limit' factory
# ./spec/factories/container_weight_limit.rb:5:in `block (2 levels) in 
<top (required)>'
# ./spec/factories/container_weight_limit.rb:4:in `block in <top 
(required)>'
# ./spec/factories/container_weight_limit.rb:3:in `<top (required)>'
# ./config/environment.rb:5:in `<top (required)>'
# ./spec/rails_helper.rb:3:in `require'
# ./spec/rails_helper.rb:3:in `<top (required)>'
# ./spec/models/container_weight_limit_spec.rb:3:in `require'
# ./spec/models/container_weight_limit_spec.rb:3:in `<top (required)>'



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

FrozenError:
 can't modify frozen Array
# ./config/environment.rb:5:in `<top (required)>'
# ./spec/rails_helper.rb:3:in `<top (required)>'
# ./spec/models/logistic_process_cost_per_product_spec.rb:3:in 
`<top          
(required)>'

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

 FrozenError:
 can't modify frozen Array
 # ./config/environment.rb:5:in `<top (required)>'
 # ./spec/rails_helper.rb:3:in `<top (required)>'
 # ./spec/models/logistic_process_cost_spec.rb:3:in `<top 
 (required)>'

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

 FrozenError:
 can't modify frozen Array
 # ./config/environment.rb:5:in `<top (required)>'
 # ./spec/rails_helper.rb:3:in `<top (required)>'
 # ./spec/models/product_freight_spec.rb:3:in `<top (required)>'

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

 FrozenError:
 can't modify frozen Array
 # ./config/environment.rb:5:in `<top (required)>'
 # ./spec/rails_helper.rb:3:in `<top (required)>'
 # ./spec/models/product_spec.rb:3:in `<top (required)>'

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

 FrozenError:
 can't modify frozen Array
 # ./config/environment.rb:5:in `<top (required)>'
 # ./spec/rails_helper.rb:3:in `<top (required)>'
 # ./spec/models/user_spec.rb:3:in `<top (required)>'

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

FrozenError:
can't modify frozen Array
# ./config/environment.rb:5:in `<top (required)>'
# ./spec/rails_helper.rb:3:in `<top (required)>'
# ./spec/services/karavel_currency_service_spec.rb:3:in `<top (required)>'
No examples found.

Finished in 0.00035 seconds (files took 7.28 seconds to load)
0 examples, 0 failures, 7 errors occurred outside of examples

/spec/factories/container_weight_limit.rb

# frozen_string_literal: true

FactoryBot.define do
 factory :container_weight_limit do
   container_size_cd 0
   bulk 22
   bag_25kg 25
   bag_50kg 25
   bigbag_900kg 26
   bigbag_1100kg 26
 end
end

config/environment.rb

 # Load the Rails application.
 require_relative 'application'

 # Initialize the Rails application.
 Rails.application.initialize!

 ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
  html_tag.html_safe
 end

Before editing, you had:

An error occurred while loading 
./spec/models/container_weight_limit_spec.rb.

Failure/Error:
 Factory.define do
  factory :offer do
   code { '003433' }
   product_id
   user_id
   crop  { '2020/06' }
   volume_unit { :ton }
   volume { 100 }
   weight_limit { 28 }
   price_unit { 400 }

This is fixed by using FactoryBot instead of Factory , something like:

An error occurred while loading 
./spec/models/container_weight_limit_spec.rb.

Failure/Error:
 FactoryBot.define do
  factory :offer do
   code { '003433' }
   product_id
   user_id
   crop  { '2020/06' }
   volume_unit { :ton }
   volume { 100 }
   weight_limit { 28 }
   price_unit { 400 }

Then, you have:

/spec/factories/container_weight_limit.rb

# frozen_string_literal: true

FactoryBot.define do
 factory :container_weight_limit do
   container_size_cd 0
   bulk 22
   bag_25kg 25
   bag_50kg 25
   bigbag_900kg 26
   bigbag_1100kg 26
 end
end

Which should be:

/spec/factories/container_weight_limit.rb

# frozen_string_literal: true

FactoryBot.define do
  factory :container_weight_limit do
    container_size_cd  {0}
    bulk               {22}
    bag_25kg           {25}
    bag_50kg           {25}
    bigbag_900kg       {26}
    bigbag_1100kg      {26}
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM