【问题标题】:How to updated nested attribute value in factory girl如何更新工厂女孩中的嵌套属性值
【发布时间】:2015-12-03 05:26:49
【问题描述】:

我们有一个工厂正在开发嵌套关系

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

我们正在创建类似

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

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

我们希望减少这种情况并仅使用

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

但我们不知道如何在这种情况下使用必填字段创建用户照片。谁能推荐一下?

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

【问题讨论】:

    标签: ruby-on-rails-3 factory-bot rspec-rails


    【解决方案1】:

    修复了创建新照片对象的问题

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多