Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RSpec测试间隔的假日期数据_Ruby On Rails_Ruby_Rspec_Factory Bot_Faker - Fatal编程技术网

Ruby on rails RSpec测试间隔的假日期数据

Ruby on rails RSpec测试间隔的假日期数据,ruby-on-rails,ruby,rspec,factory-bot,faker,Ruby On Rails,Ruby,Rspec,Factory Bot,Faker,我正在使用Faker和Factory_bot Gems为我的RSpec测试生成一些假数据,我需要为每个测试生成1小时的间隔,例如: 约会模型有开始日期和结束日期,它们应该相差一小时。例如: 开始日期:“2020-10-20 19:51:00” 结束日期:“2020-10-20 20:51:00” 这是我目前的工厂: factory :appointment do start_date { Faker::Date.between(from: 2.year.ago, to: Date.t

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

约会模型有开始日期和结束日期,它们应该相差一小时。例如:

开始日期:“2020-10-20 19:51:00” 结束日期:“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的一些技巧

。今天使用时区。这些是时间,而不是日期,所以
时间。当前的
更合适。最后,除非您所有的约会都是在过去,否则请使用
2.year.since

时间戳的约定是在
处以
\u结尾<代码>在
处开始,在
处结束。这也避免了对
start\u date
这是时间而不是日期的混淆


我们可以利用和将其添加到开始日期<代码>结束日期{开始日期+1小时}

与将特定测试的假设硬编码到工厂中不同,我们可以使用a来明确假设

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)
有个问题。如果调用者更改了结尾怎么办?那么起始点应以结束点为基础。但是,如果他们将开始设置为结束,则需要以开始设置为基础。这导致了循环定义

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

  # Circular
  start_at { end_at + duration }
  end_at { start_at - duration }
end
我们需要使用a来避免循环依赖

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。这将使用哪一个轨道将在两次之间转变为一个轨道。这比开始和结束时间戳更容易使用

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))

start\u date
end\u date
是日期还是时间?