How to update the value of a nested attribute in a factory girl

we have one factory in which we develop a 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 create an attribute of type

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

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

we want 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 going to create a user photo with a required field in this scenario. Can anyone suggest?

Looking something like  users = FactoryGirl.create(:user_with_photos,[update user photo attribute make mandatory true ] campaign: @campaign)
+4
source share
1 answer

Fixed problem with creating a new photo object

photos = FactoryGirl.create_list(:photo, 12, mandatory: true)
users = FactoryGirl.create(:use, :photos, campaign: @campaign)
+2
source

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


All Articles