(Rails) 测试在深夜失败
(Rails) Test fails during late night time
我的 rails 应用程序包含许多测试。所有测试都在常规情况下通过。即不包括深夜的时候。
实际上有一些测试在晚上失败了。所有这些测试都涉及修改模型的时间属性并查看相关模型是否受到影响。
test "changing time should affect hours" do
// ..User is loaded.
user.attend(event)
assert_equal 1, user.hours // User attends a 1 hour event that has passed.
// Move event to the future.
event.update_attributes(date: Date.today,
start_time: Time.now,
end_time: Time.now + 1.hour)
assert_equal 0, attendance_of(user).hours // Passes in day, fails during night
end
test "valid event creation" do
// Count does NOT change by 1 at night.
assert_difference '@group.events.count', 1 do
post group_events_path(@group), event: { ...
date: Date.today,
start_time: Time.now,
end_time: Time.now + 1.hour,
... }
end
end
这是怎么回事?作为参考,这是我目前用来确定何时更新 attendance(这是 event 具有的内容)的方法。这来自事件控制器:
def not_ended?
date.future? || (date.today? &&
(Time.now.seconds_since_midnight < end_time.seconds_since_midnight))
end
def update_attendances
// ... Determine the new date, start, and end time values through ActiveRecord::Dirty
if not_ended?
remove_checks = true
end
attendances.each do |attendance|
new_checked = remove_checks ? false : attendance.checked
attendance.update_attributes(went: new_start, left: new_end,
checked: new_checked)
end
end
end
验证事件以确保其时间不奇怪:
def valid_time
if start_time == end_time
// Error...
end
if start_time > end_time
// Error...
end
end
时区 application.rb:
config.time_zone = 'Pacific Time (US & Canada)'
您的 not_ended?
方法已损坏。当事件在午夜之前开始但在午夜之后结束时,它不起作用。在这种情况下,日期是今天(假设数据基于开始时间),但自事件结束午夜以来的秒数小于当前时间。
在这些情况下,您不应该尝试分别处理日期和时间。您应该有办法检索事件结束的日期时间并将其与当前日期时间进行比较。
使用 Time.zone.now 或 timecop.freeze 冻结时区,因为您的测试可能使用的是系统时间而不是 rails 应用配置时间
我的 rails 应用程序包含许多测试。所有测试都在常规情况下通过。即不包括深夜的时候。
实际上有一些测试在晚上失败了。所有这些测试都涉及修改模型的时间属性并查看相关模型是否受到影响。
test "changing time should affect hours" do
// ..User is loaded.
user.attend(event)
assert_equal 1, user.hours // User attends a 1 hour event that has passed.
// Move event to the future.
event.update_attributes(date: Date.today,
start_time: Time.now,
end_time: Time.now + 1.hour)
assert_equal 0, attendance_of(user).hours // Passes in day, fails during night
end
test "valid event creation" do
// Count does NOT change by 1 at night.
assert_difference '@group.events.count', 1 do
post group_events_path(@group), event: { ...
date: Date.today,
start_time: Time.now,
end_time: Time.now + 1.hour,
... }
end
end
这是怎么回事?作为参考,这是我目前用来确定何时更新 attendance(这是 event 具有的内容)的方法。这来自事件控制器:
def not_ended?
date.future? || (date.today? &&
(Time.now.seconds_since_midnight < end_time.seconds_since_midnight))
end
def update_attendances
// ... Determine the new date, start, and end time values through ActiveRecord::Dirty
if not_ended?
remove_checks = true
end
attendances.each do |attendance|
new_checked = remove_checks ? false : attendance.checked
attendance.update_attributes(went: new_start, left: new_end,
checked: new_checked)
end
end
end
验证事件以确保其时间不奇怪:
def valid_time
if start_time == end_time
// Error...
end
if start_time > end_time
// Error...
end
end
时区 application.rb:
config.time_zone = 'Pacific Time (US & Canada)'
您的 not_ended?
方法已损坏。当事件在午夜之前开始但在午夜之后结束时,它不起作用。在这种情况下,日期是今天(假设数据基于开始时间),但自事件结束午夜以来的秒数小于当前时间。
在这些情况下,您不应该尝试分别处理日期和时间。您应该有办法检索事件结束的日期时间并将其与当前日期时间进行比较。
使用 Time.zone.now 或 timecop.freeze 冻结时区,因为您的测试可能使用的是系统时间而不是 rails 应用配置时间