FactoryBot Undefined Method Error
Asked Answered
T

3

5

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.

Transfix answered 15/3, 2018 at 16:43 Comment(5)
Does your Order model have an order_provider attribute?Reger
If it didn't, would it not provide a generic undefined method error rather than telling me to try order_provider? From, the console, I can call order.order_provider and get what I need.Transfix
OK, so it seems you may have a getter (order_provider) but not a setter (order_provider=). Please edit your question to show the code for your Order model.Reger
@IainK it did give you a generic NoMethodError just so happens did_you_mean found a similar method name and offered a suggestion. Just like "S".chimp asks me if I meant chomp or chomp!Sauer
It's almost certain you haven't defined #order_provider= (it's a different method than #order_provider).Lebel
V
7

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.

Vegetation answered 16/7, 2019 at 14:54 Comment(0)
P
3

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

Pinter answered 15/10, 2020 at 16:27 Comment(0)
P
0

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)
Paddock answered 15/3, 2018 at 16:51 Comment(3)
I agree this is a good use case for a trait, but it's not going to work if there is no method for Order#order_provider=.Reger
There is no order_provider= method. Look at the OP's error message.Reger
It certainly is possible, but uncommon if order_provider is not an attribute on model.Paddock

© 2022 - 2024 — McMap. All rights reserved.