Rails工厂女孩如何为这组相关模型创建工厂

时间:2016-04-27 17:59:41

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

我有一个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

基本上 - 我的日子必须有时间段才有意义,即一天可以有以下有效:

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

似乎序列可以在这里帮助我...如果我有一个全球性的话,这将很容易。一组开始分钟,开始时间等,因为我可以从阵列中选择下一个,但我不知道这是否可行或是否有更好的方法?

请帮忙!

1 个答案:

答案 0 :(得分: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与n一致。否则,您可以使用n尝试更大的循环。