Factory Girl passes parameter

I banged my head about this. I want to be able to overwrite attributes on top of signs. I read the documentation and some internet examples, but I can't get it to work.

This is 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

+5
source share
1 answer

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

You should also evaluate start_time when creating a Factory instance by placing it in braces. Otherwise, this will be evaluated before the launch is initialized.

Try 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 this:

 create( :instant_instance, start: 1.hours.ago, :active_now) 
+8
source

Source: https://habr.com/ru/post/1258688/


All Articles