我已经在 Apt 模型上创建了一个自定义验证,如果表单提交是在下午 3 点之后完成,则验证不能在第二天预订约会,我正在尝试确定运行单元测试的最佳方式验证我的自定义验证是否正常工作。
目前,我的测试包含一个 if 语句来检查测试是在下午 3 点之前还是之后运行,并根据当前系统时间使用不同的测试。但是,无论系统时间是多少,我都希望能够测试这两种情况。
test "booking appointment for tomorrow before/after three pm" do
a = appointments(:aptone)
# Set appointment date to tomorrow
a.apt_date = DateTime.current + 1.day
if (Time.current.hour >= 12)
assert(a.invalid?, 'Appointment CANNOT be booked after 3 PM')
elsif
assert(a.valid?, 'Appointment CAN be booked before 3 PM')
end
end
我想知道是否有办法在运行测试时临时设置系统时间,以便无论何时运行测试,我都可以测试这两个断言。