【发布时间】:2014-02-17 16:58:25
【问题描述】:
我正在尝试(但失败了!)以下代码来比较 RSpec 中的两个 Time 对象:
describe '#current_shift_datetime' do
subject { WipStack.current_shift_datetime }
it "To be a Time object" do
expect(subject).to be_kind_of(Time)
end
it "To return current shift datetime" do
# Trying to figure out why they are not equal
puts subject.in_time_zone.round
puts 0.day.ago.midnight.in_time_zone.round
# Problematic code here -->
expect(subject.in_time_zone.round).to be_equal(0.day.ago.midnight.in_time_zone.round)
# End of problematic code
end
end
我在互联网上阅读了几篇关于 rspec 时间比较的文章,其中一篇解释了毫秒的问题(因此是回合),另一页则讨论了 stub,但后来我以 An error occurred in an after hook SystemStackError: stack level too deep 结束。
测试的输出是:
1) WipStack#current_shift_datetime To return current shift datetime
Failure/Error: expect(subject.in_time_zone.round).to be_equal(0.day.ago.midnight.in_time_zone.round)
expected equal?(Mon, 17 Feb 2014 00:00:00 CST -06:00) to return true, got false
puts 输出:
#current_shift_datetime
To be a Time object
2014-02-17 00:00:00 -0600
2014-02-17 00:00:00 -0600
To return current shift datetime (FAILED - 1)
更新:
这里是current_shift_datetime 方法:
def WipStack.current_shift_datetime(scope = nil)
shifts = WipStack.get_shifts(scope)
current_shift = (Time.zone.now - 1.day).strftime("%m/%d/%Y")+" "+shifts.last
current_time = Time.zone.now.strftime("%H:%M")
shifts.each do |shift|
if current_time > shift
current_shift = Time.zone.now.strftime("%m/%d/%Y")+" "+shift
end
end
Time.zone.parse(current_shift)
end
【问题讨论】:
-
我们来看看
current_shift_datetime方法 -
谢谢@CharlesJHardy 这是代码
标签: ruby-on-rails ruby ruby-on-rails-3 rspec