我想下面的代碼在RSpec中比較兩個時間對象:RSpec的預期等於(Time對象)返回true,得到了假
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
的所說輸出:
#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
感謝@CharlesJHardy這裏是代碼 –