带有RSpec测试间隔的伪造日期数据



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

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

开始日期:'2020-10-20 19:51:00'end_date:'2020-10-2020: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技巧。

不要在Rails中使用Date.today,它不知道时区。使用Time.zone.today。这些是时间,而不是日期,所以Time.current更合适。最后,除非您的所有约会都是过去的,否则请使用2.years.since

时间戳的约定是以_at结束。6和CCD_ 7。这也避免了对start_date的混淆,CCD_8是时间,而不是日期。


我们可以利用ActiveSupport::Duration及其数字扩展来添加到start_date。end_date { start_date + 1.hour }

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

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)

我们可以做得更好。如果我们想要不同的持续时间呢?使用瞬态属性而不是特征。这使您可以向工厂发送一个不是对象属性的属性。喜欢持续时间。

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

我们需要使用回调来避免循环依赖。

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,可以将start_atend_at合并到一个范围列中。这使用Postgres的tstzrange类型,Rails会将其转换为两次之间的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))

相关内容

  • 没有找到相关文章

最新更新