rom-factory
rom-factory copied to clipboard
Trait and dependent attributes conflicting
Describe the bug
When using the dependent attribute feature of factories there is an unforeseen result. If the method that is being defined is created as a part of the trait and another method uses the dependent attribute feature to access that value the result of said method corresponds with the parent factory object and not the trait. This can be fixed by defining the method value directly in the creation of the factory object. But should also work in the previous statement.
To Reproduce
Factory.define(:store) do |f|
f.name { fake(:company, :name) }
f.location_name { |name| "#{name} west" }
end
f.trait :new_store do |t|
t.name { 'New Store' }
end
create :store
result = { name: 'Random Store Name, location_name: 'Random Store Name West'} #Correct
create :store, :new_store
result = { name: 'New Store', location_name: 'Random Store Name West'} # Incorrect
HOWEVER
create :store, :new_store, name: 'Explicit Store Name'
result = { name: 'Explicit Store Name', location_name: 'Explicit Store Name West'} #Correct
Expected behavior
Factory.define(:store) do |f|
f.name { fake(:company, :name) }
f.location_name { |name| "#{name} west" }
end
f.trait :new_store do |t|
t.name { 'New Store' }
end
create :store
result = { name: 'Random Store Name, location_name: 'Random Store Name West'}
create :store, :new_store
result = { name: 'New Store', location_name: 'New Store West'}
My environment
- Affects my production application: YES
- Ruby version:2.7.4
- OS: Mac