Factory Girl pass parameter to trait
Asked Answered
R

1

19

I've been banging my head over this. I want to be able to overwrite attributes on top of traits. I've been reading the documentation and some internet examples but I can't get it to work.

This is sort of what I want to do:

test "..." do 
 #overwrite start_time
 middle = create( :basic_instant_instance, start: 1.hours.ago)
end 

FactoryGirl.define do

  factory :instant_instance do

  trait :active_now do 
    #attributes...
    transient do
      start nil
    end  
    #overwrite start_time
    start_time start.present? ? start : Time.now
  end

  factory :basic_instant_instance,          traits: [:active_now] 
end   

I keep getting:

ArgumentError: Trait not registered: start

Recrudescence answered 24/10, 2016 at 0:5 Comment(0)
D
21

You need to rethink your strategy a little here - you have a basic_instant_instance which does not inherit from instant_instance and therefore knows nothing about the trait [:active_now] or start attribute.

You also need to be evaluating the start_time at the time you are building the Factory instance by placing it in curly brackets. Otherwise it will be evaluated before start is initialised.

Try something like the following:

FactoryGirl.define do
  factory :instant_instance do
    trait :active_now do 
      # attributes...

      transient do
        start { nil }
      end

      # overwrite start_time
      start_time { start.present? ? start : Time.now }
    end
  end
end

and then call it like so:

create(:instant_instance, :active_now, start: 1.hours.ago)
Dawson answered 24/10, 2016 at 0:43 Comment(5)
Seems I was wrong about not being able to pass transient data through to a trait - have amended my solution accordingly and also realised you need the curly braces to evaluate correctlyDawson
It also works with : create( :basic_instant_instance, start: 1.hours.ago). By the way, I don't understand : "a basic_instant_instance ... does not inherit from instant_instance and therefore knows nothing about the trait [:active_now] or start attribute."Recrudescence
rather than trying to explain it in a comment better if you visit inheritance. I could add further information to my answer if necessary.Dawson
and what do you mean it also works with create( :basic_instant_instance, start: 1.hours.ago) ... i thought you were getting a trait not registered error.Dawson
yes, but It seems to have been caused by the curly brackets, as you suggested in your answer :)Recrudescence

© 2022 - 2024 — McMap. All rights reserved.