rom-rb / rom-factory

Data generator with support for persistence backends
MIT License
83 stars 42 forks source link

Trait and dependent attributes conflicting #70

Open Rogwzrd opened 2 years ago

Rogwzrd commented 2 years ago

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