Ruby on rails 和工厂女工有很多联系

Ruby on rails 和工厂女工有很多联系,ruby-on-rails,factory-bot,Ruby On Rails,Factory Bot,我的位置模型有一个多个:用户关联,我如何将用户和用户2添加到我工厂的集合中 此外,在构建对象图时,factory_girl是否应该仅用于创建帐户、用户,而不是实际构建两者之间的关系?我应该在我的测试中自己做这件事,还是做一些帮助? 因为假设我要创建account1和account2,每个account都有一个位置。我必须在我的测试中自己构建这个对象图,对吗?您必须使用之后的回调: factory :account do end factory :user do account end

我的位置模型有一个
多个:用户关联,我如何将用户和用户2添加到我工厂的集合中

此外,在构建对象图时,factory_girl是否应该仅用于创建帐户、用户,而不是实际构建两者之间的关系?我应该在我的测试中自己做这件事,还是做一些帮助?
因为假设我要创建account1和account2,每个account都有一个位置。我必须在我的测试中自己构建这个对象图,对吗?

您必须使用
之后的
回调:

factory :account do
end

factory :user do 
  account
end

factory :user2 do 
  account
end


factory :location do
  // how to add to the users has_many collection?
end

请参阅

您必须在
回调后使用

factory :account do
end

factory :user do 
  account
end

factory :user2 do 
  account
end


factory :location do
  // how to add to the users has_many collection?
end

请参见

您可以一次创建两个用户:

factory :location do
  after(:create) do |location, evaluator|
    create(:user, location: location)
    create(:user2, location: location)
  end
end

您可以一次创建2个用户:

factory :location do
  after(:create) do |location, evaluator|
    create(:user, location: location)
    create(:user2, location: location)
  end
end