简体   繁体   中英

FactoryBot Undefined Method Error

I have an existing Factory (order), and I am trying to make a new factory that effectively inherits from it. It looks like this:

factory :order_with_domain, :parent => :order do |o|
  o.order_provider 'DomainNameHere'
end

Upon doing that and running the specs with order_with_domain , I am greeted by this:

undefined method `order_provider=' for #<Order:0x00007fc70d9fafc0> 
Did you mean?  order_provider

I receive this same error if I try and place order_provider in the parent Factory.

Any helps is much appreciated.

Thanks.

Try running rails c test then check if your column is present. If not then it's an issue with your test database and you need to run your migrations in the test environment using RAILS_ENV=test rake db:migrate . If nothing happens, delete your schema.rb then run the migrations command again.

Try putting the value in curly braces like so:

factory :order_with_domain, :parent => :order do |o|
  o.order_provider { 'DomainNameHere' }
end

Here is the reason on thoughtbot

Assuming your model has an order_provider attribute or order_provider= method, as @moveson commented above.

I would use traits . Something like this:

factory :order do
  # ... original factory stuff

  trait :with_domain do
    order_provider 'DomainNameHere'
  end
end

Then to use it:

order_with_domain = FactoryBot.create(:order, :with_domain)

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