代码之家  ›  专栏  ›  技术社区  ›  Kees Briggs

factrybot:如何在skip中使用transient创建工厂以指定内部工厂中的属性?

  •  0
  • Kees Briggs  · 技术社区  · 6 年前

    我有下面的工厂,里面有“跳过创建”,也在创建时调用另一个工厂,我试图在这个工厂上指定一个特定的UUID。例子:

    FactoryBot.define do
      factory :experiment do
    
        transient { order nil }
    
        env_array = %w[prod dev test]
        uuid { SecureRandom.uuid }
        name { 'some name' }
    
        skip_create
        initialize_with do
          env_array.each_with_index do |env, idx|
            FactoryBot.create(:environment, uuid: 'b5c096d5-479a-4693-ac14-9cea7dfd045c') if order.eql? 'first'
          end
        end
      end
    end
    

    问题是,我不能 秩序 可采取行动。我得到:

    参数错误:特征未注册:顺序

    我怎样才能得到 秩序 当我打电话给工厂时可以指定?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Kedarnag Mukanahallipatna    6 年前

    因为你错过了花括号 {}

    FactoryBot.define do
      factory :experiment do
        transient do
          order { nil }
        end
       ....
      end
    end