相关疑难解决方法(0)

FactoryGirl:填充有很多关系保存构建策略

我的问题似乎很常见,但我没有在文档或互联网本身找到任何答案.

它似乎是这个问题的一个克隆has_many,同时尊重factory_girl中的构建策略,但在post-factory_girl改变了很多之后的2.5年.

我有一个名为照片的has_many关系的模型.我想填充这个有很多关系,保留了我对构建策略的选择.

如果我打电话,offering = FactoryGirl.build_stubbed :offering, :stay我希望offering.photos成为一个存根模型的集合.

我发现实现这一目标的唯一方法是:

factory :offering do
  association :partner, factory: :named_partner
  association :destination, factory: :geolocated_destination

  trait :stay do
    title "Hotel Gran Vía"
    description "Great hotel in a great zone with great views"
    offering_type 'stay'
    price 65
    rooms 70
    stars 4
    event_spaces 3
    photos do
      case @build_strategy
      when FactoryGirl::Strategy::Create then [FactoryGirl.create(:hotel_photo)]
      when FactoryGirl::Strategy::Build then [FactoryGirl.build(:hotel_photo)]
      when FactoryGirl::Strategy::Stub then [FactoryGirl.build_stubbed(:hotel_photo)]
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

没必要说IT必须存在更好的方法.

想法?

testing ruby-on-rails factory-bot

9
推荐指数
2
解决办法
3318
查看次数

标签 统计

factory-bot ×1

ruby-on-rails ×1

testing ×1