【问题标题】:Rails factory girl how can I create a factory for this set of associated modelsRails 工厂女孩如何为这组关联模型创建工厂
【发布时间】:2016-04-27 17:59:40
【问题描述】:

我有一个 Day 模型,一个 day 模型可以有多个 time_slots。

日模型如下:

# == Schema Information
#
# Table name: days
#
#  id          :integer          not null, primary key
#  schedule_id :integer
#  created_at  :datetime         not null
#  updated_at  :datetime         not null
#  wday        :integer
#

class Day < ActiveRecord::Base
  belongs_to :schedule
  has_many :time_slots, :dependent => :destroy
end

TimeSlot 模型如下所示:

# == Schema Information
#
# Table name: time_slots
#
#  id           :integer          not null, primary key
#  day_id       :integer
#  created_at   :datetime         not null
#  updated_at   :datetime         not null
#  pin          :string
#  start_hour   :integer
#  start_minute :integer
#  end_hour     :integer
#  end_minute   :integer
#

class TimeSlot < ActiveRecord::Base
  belongs_to :day
  has_many :passes, :dependent => :destroy

  validates :start_hour, :presence => true

  validates :start_minute, :presence => true

  validates :end_hour, :presence => true

  validates :end_minute, :presence => true
end

我目前有以下工厂:

# == Schema Information
#
# Table name: days
#
#  id          :integer          not null, primary key
#  schedule_id :integer
#  created_at  :datetime         not null
#  updated_at  :datetime         not null
#  wday        :integer
#

FactoryGirl.define do
  factory :day do
    wday rand(0..6)

  after(:create) do |day, evaluator|
        create_list(:time_slots, 2, day: day,start_hour: generate :start_hour, start_min: generate :start_min, end_hour: generate :end_hour,end_min: generate :end_min)
  end

  end

  sequence :start_hour do |n|
    #Needs to return something between 00 and 23
    n
  end

  sequence :start_min do |n|
    #Needs to return something between 00 and 55
    n
  end

  sequence :end_hour do |n|
    #Needs to return something between 00 and 23 but a value HIGHER than start_hour
    #If end_min == start_min
    n
  end

  sequence :end_min do |n|
    #Needs to return something between 00 and 55 but a value that is definitely after start_min if end_hour and start_hour are equal 
    n
  end



end

基本上 - 我的一天必须有有意义的 time_slots,即一天可能有以下有效:

Timeslot 1
start_hour : 10
start_min : 00
end_hour : 11
end_min : 00

Timeslot 2
start_hour : 11
start_min : 00
end_hour : 12
end_min : 00

但是这将是无效的,因为时隙 2 与时隙 1 重叠。同样 2 个相同的 time_slots 无效:

Timeslot 1
start_hour : 10
start_min : 00
end_hour : 11
end_min : 00

Timeslot 2
start_hour : 10
start_min : 30
end_hour : 11
end_min : 00

似乎序列在这里可以帮助我...如果我有一组“全局”的开始时间、开始时间等,这将很容易,因为我可以从数组中选择下一个但我没有不知道这是否可能或是否有更好的方法?

请帮忙!

【问题讨论】:

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


    【解决方案1】:

    实际上,我认为我不会为此使用 factory_girl。相反,我会明显在测试中创建我的时间段。

    但是,你可以这样做:

    sequence :start_hour do |n|
      #Needs to return something between 00 and 23
      raise 'No more timeslots available' if n == 24
      n - 1 # if factory_girl starts with 1
    end
    
    sequence :start_min do |n|
      #Needs to return something between 00 and 55
      0 # Just always zero
    end
    
    sequence :end_hour do |n|
      #Needs to return something between 00 and 23 but a value HIGHER than    start_hour
      #If end_min == start_min
      n - 1
    end
    
    sequence :end_min do |n|
      #Needs to return something between 00 and 55 but a value that is definitely after start_min if end_hour and start_hour are equal 
      55 # like always 55 minute slots
    end
    

    所以基本上,您可以使用n。

    希望 factory_girl 与ns 一致。否则,您可以使用n 尝试更大的循环。

    【讨论】:

    • 那么什么是n...数字?它不会增加吗? n-1 不会将 n 减 1...意味着 start_hour 和 end_hour 可能仍然相同吗? n 是否在所有序列中都是全局的,所以保证在所有序列生成器中都是唯一的?
    • n 每次调用 factory_girl 时都会增加,例如“create”。
    • 好吧,当然,我还没有测试过它,但如果没有,那就很奇怪了。实际上,我只是从您的问题中复制过来的。
    • 但在这种情况下,我们最终会得到 n = 0 所以 -1 表示开始时间,0 表示 start_min ,-1 表示结束小时 55 表示结束分钟......
    • 好吧,抱歉……我刚看到。您将这些序列块放入您的day 工厂。它们应该以“1”开头。
    猜你喜欢
    • 2010-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多