具有 RSpec 测试间隔的假日期数据

Fake date data with intervals for RSpec testing

我正在使用 Faker 和 Factory_bot Gems 为我的 RSpec 测试生成一些假数据,我需要为每个测试生成 1 小时的间隔,例如:

Appointment 模型有 start_date 和 end_date,它们应该相差一小时。例如:

start_date: '2020-10-20 19:51:00' end_date: '2020-10-20 20:51:00'

这是我现在的工厂:

  factory :appointment do
    start_date { Faker::Date.between(from: 2.year.ago, to: Date.today) }
    end_date { Faker::Date.between(from: 2.year.ago, to: Date.today) }
    user_id nil
    therapist_id nil
  end

我想知道如何存储第一个生成的假数据并添加一个小时。

首先是一些 FactoryBot 和 Rails 提示。

Do not use Date.today in Rails, it is not aware of time zones。使用 Time.zone.today。这些是时间,而不是日期,所以 Time.current 更合适。最后,除非您所有的约会都是过去的,否则请使用 2.years.since.

时间戳的约定是以 _at 结尾。 start_atend_at。这也避免了混淆 start_date 这是一个时间,而不是一个日期。


我们可以利用ActiveSupport::Duration and its Numeric extensions添加到start_date。 end_date { start_date + 1.hour }.

我们可以使用 trait 来明确假设,而不是将特定测试的假设硬编码到工厂中。

factory :appointment do
  # These are the normal conditions.
  # end_at will be 15 to 180 minutes after start_at.
  start_at { Faker::Time.between(from: 2.years.ago, to: 2.years.since) }
  end_at { start_at + rand(15..180).minutes }

  # This is a specific trait putting end_at an hour after start_at.
  trait :in_one_hour do
    end_at { start_at + 1.hour }
  end
end

# An appointment of 1 hour which started yesterday
appointment = build(:appointment, :in_one_hour, start_at: 1.day.ago)

我们可以做得更好。如果我们想要不同的持续时间怎么办?使用 transient attribute 而不是特征。这使您可以将不是对象属性的属性发送到工厂。喜欢时长。

factory :appointment do
  transient do
    duration { rand(15..180).minutes }
  end

  start_at { Faker::Time.between(from: 2.years.ago, to: 2.years.since) }
  end_at { start_at + duration }
end

# An appointment with a random but reasonable duration.
p build(:appointment)

# An appointment with a duration of exactly 1 hour.
p build(:appointment, duration: 1.hour)

# An appointment lasting 30 minutes starting yesterday.
p build(:appointment, duration: 30.minutes, start_at: 1.day.ago)

有问题。如果调用者更改 end_at 怎么办?那么 start_at 应该基于 end_at。但是,如果他们设置 start_at,则 end_at 需要基于 start_at。这导致循环定义。

factory :appointment do
  transient do
    duration { rand(15..180).minutes }
  end

  # Circular
  start_at { end_at + duration }
  end_at { start_at - duration }
end

我们需要使用一个callback来避免循环依赖。

factory :appointment do
  transient do
    duration { rand(15..180).minutes }
  end
      
  after(:build) do |appointment, evaluator|
    case
    when appointment.start_at && appointment.end_at
      # The user set both, leave them be.
    when appointment.start_at
      # The user set only the start_at.
      appointment.end_at ||= appointment.start_at + evaluator.duration
    when appointment.end_at
      # The user set only the end_at.
      appointment.start_at ||= appointment.end_at - evaluator.duration
    else
      # The user set neither.
      appointment.start_at = Faker::Time.between(from: 2.years.ago, to: 2.years.since)
      appointment.end_at = appointment.start_at + evaluator.duration
    end
  end
end

p build(:appointment)
p build(:appointment, duration: 1.hour, start_at: 1.year.ago)
p build(:appointment, duration: 1.hour, end_at: 1.year.since)
p build(:appointment, start_at: 1.year.ago, end_at: 1.year.since)

最后,如果您使用的是 Postgres,you can merge start_at and end_at into a single range column. This uses Postgres's tstzrange type which Rails will turn into a Range 两次之间。这比开始和结束时间戳更容易使用。

factory :appointment do
  transient do
    duration { rand(15..180).minutes }
  end
        
  timespan do
    start_time = Faker::Time.between(from: 2.years.ago, to: 2.years.since)
    end_time = start_time + duration
    (start_time..end_time)
  end
end

p FactoryBot.build(:appointment)
p FactoryBot.build(:appointment, duration: 1.hour)
p FactoryBot.build(:appointment, timespan: (1.year.ago..Time.current))