In my spec I need to verify whether strings are being converted correctly to other types. Among these are Time, Date and DateTime.
The tests work fine with all types including Date, but it fails when Time is involved:
let(:type) { DateTime }
let(:date_time) { type.now }
let(:params) { [ date_time.to_s ] }
it 'should convert the parameters to dates/times' do
# converted basically calls params.map { |param| type.parse param }
converted.should == [date_time]
end
This actually almost works:
expected: [#<DateTime: 2012-01-17T15:03:27-02:00 ((2455944j,61407s,568873440n),-7200s,2299161j)>]
got: [#<DateTime: 2012-01-17T15:03:27-02:00 ((2455944j,61407s,0n),-7200s,2299161j)>] (using ==)
The diff is off by only one value, but I don’t know what exactly it means. The result of the Time test is even more confusing:
expected: [2012-01-17 15:03:27 -0200]
got: [2012-01-17 15:03:27 -0200] (using ==)
The only differences between the tests are the type and the description. After some tracing, it appears to me that the parsed times have no microsecond precision; usec always returns 0.
Any tips on how to make these specs pass?
Try comparing their string representations using a formatting which excludes the fields that you do not want to compare (e.g. millis, micros, etc).
[Edit] Note that the above example compares the two DateTimes without any fractional seconds (see the linked
iso8601docs above).If you really want to compare the DateTime objects themselves then your test code is complete, you’ll just have to fix your implementation to adjust the fractional seconds part to some fixed value (e.g. zero).