如何在嵌套工厂中创建条件属性

时间:2016-04-12 14:35:58

标签: ruby-on-rails factory-bot

我有一些工厂,但我需要传递一个论点或特征,但我不确定如何。我需要在hash_is_met中创建一个额外的字段。

  sequence :assets do |n|
    assets = []
    count = (n % 3) + 1
    while count > 0
      count -= 1
      i = (n + count) % asset_compound_ids.length
      compound_id = asset_compound_ids[i]
      asset_id, blob_name = compound_id.split(/,/)
      assets << {id: asset_id.to_i, blob: {name: blob_name, url: ASSET_ID_2_URL[compound_id]}}

      if condition_is_met
        assets[index][:blob][:asset_duration_ms] = 34564224
      end
    end   

    assets
  end

factory :layout do
    assets
end

factory :event do
    layout
end

我们可以将条件传递给序列吗?

1 个答案:

答案 0 :(得分:1)

如何向工厂添加回调?

factory :layout do
  assets
  after(:build, :stub) do
    next unless condition_is_met
    assets[index][:blob][:asset_duration_ms] = 34564224
  end