mercredi 2 décembre 2015

How to updated nested attribute value in factory girl

we have one factory in which we are developing nested relation

FactoryGirl.define do
  factory : user do
    name "Arvind"
    association :campaign
    factory :user_with_photos do
      after(:build) do |user|
        user.photos.build name: 'user 1', 
        user.photos.build name: 'user 2',  position: 2
      end
    end
    factory :user_with_mandatory_photos do
      after(:build) do |user|
        user.photos.build name: 'user 3',  mandatory: true
        user.photos.build name: 'user 4',  mandatory: true
      end
    end

    factory :user_with_mandatory_photos do
      after(:build) do |user|
        user.photos.build name: 'user 5',  mandatory: false
        user.photos.build name: 'user 6',  mandatory: false
      end
    end
  end
end

we are creating attribute like

 users = FactoryGirl.create(:user_without_mandatory_fields, campaign: @campaign)

 users = FactoryGirl.create(:user_with_mandatory_fields, campaign: @campaign)

we are looking to reduce this and use only

factory : user do
    name "Arvind"
    association :campaign
    factory :user_with_photos do
      after(:build) do |user|
        user.photos.build name: 'user 1', 
        user.photos.build name: 'user 2',  position: 2
      end
    end
end

But we are not getting how to create user photo with the mandatory field in this scenario. Can anyone please suggest?

Looking something like  users = FactoryGirl.create(:user_with_fields,[update user photo attribute make mandatory true ] campaign: @campaign)

Aucun commentaire:

Enregistrer un commentaire