Trait with jus one attribute different

Viewed 47

I have a trait in factory something like this

    trait :trial_with_address_info do
      first_name {'User'}
      last_name {'Name'}
      email {'trial.user@gmail.com'}
      role {'user'}
      tableau_username {'TrialUser'}
      association :organisation, factory: [:trial_organisation]
      association :user_group, factory: [:generic_user_group]
      address {'address'}
      city {'city'}
      state {'State'}
      zip {'123'}
      country {'code'}
    end

I want to crate another trait with all the same properties and values like the above trait with just one additional attribute.

Whats the recommended way to do this? or should i just repeat the same attribute + the one i need in the new trait?

Thanks.

1 Answers

Use trait within a trait feature

    trait :trial_with_address_info do
      first_name {'User'}
      last_name {'Name'}
      email {'trial.user@gmail.com'}
      role {'user'}
      tableau_username {'TrialUser'}
      association :organisation, factory: [:trial_organisation]
      association :user_group, factory: [:generic_user_group]
      address {'address'}
      city {'city'}
      state {'State'}
      zip {'123'}
      country {'code'}
    end

    trait :john_with_address_info
      trial_with_address_info
      first_name {'John'}
    end

But it also depends on what you want, if you need to create objects of the same trait but different e-mails (because of validations) the recommended way would be to use sequences

    trait :trial_with_address_info do
      # ...
      sequence(:email) { |n| "trial.user.#{n}@gmail.com"}
      # ...
    end

So the recommended way would really depend on "why" you think you need another trait.

Related