I''m having an issue comparing two seemingly equivalent dates:
spec:
it "sets the user''s session_key_updated_at" do
now = Time.zone.now
Timecop.freeze(now) do
verify_user
@verified_user.session_key_updated_at.should == now
end
end
output:
sets the user''s session_key_updated_at
expected: Fri, 21 Aug 2009 16:08:51 UTC +00:00,
got: Fri, 21 Aug 2009 16:08:51 UTC +00:00 (using ==)
They''re identical, no?
They''re the same class, too:
now.class:
ActiveSupport::TimeWithZone
@verified_user.session_key_updated_at.class: ActiveSupport::TimeWithZone
Any ideas on what''s going on or what I''m doing wrong?
For the record:
> spec -v
rspec 1.2.7
> script/about
About your application''s environment
Ruby version 1.8.6 (i686-darwin9.1.0)
RubyGems version 1.3.5
Rack version 1.0
Rails version 2.3.2
Active Record version 2.3.2
Action Pack version 2.3.2
Active Resource version 2.3.2
Action Mailer version 2.3.2
Active Support version 2.3.2
Environment development
Database adapter sqlite3
Database schema version 20090812181104
Thanks,
/g
--
George Anderson
BenevolentCode LLC
george at benevolentcode.com
On 21 Aug 2009, at 17:15, George Anderson wrote:> expected: Fri, 21 Aug 2009 16:08:51 UTC +00:00, > got: Fri, 21 Aug 2009 16:08:51 UTC +00:00 (using ==) > They''re identical, no?Not necessarily -- Time instances are microsecond precision, so two of them can differ (in microseconds) but have the same string representation (which only shows seconds). See what the #usec method gives you for each of the times. Cheers, -Tom
Spot on, Tom. Thanks.
For the (google) record:
Time.usec Returns just the number of microseconds for the given time.
ActiveRecord datetime attributes ignore microseconds (at least with
the SQLite adapter). In my case:
now.usec: 991786
@verified_user.session_key_updated_at.usec: 0
To fix the spec, don''t use Time.now for the comparison:
it "sets the user''s session_key_updated_at" do
now = Time.zone.local(2008, 9, 1, 12, 0, 0)
Timecop.freeze(now) do
verify_user
@verified_user.session_key_updated_at.should == now
end
end
Cheers,
/g
--
George Anderson
BenevolentCode LLC
O: (410) 461-7553
C: (410) 218-5185
george at benevolentcode.com
On Fri, Aug 21, 2009 at 12:26 PM, Tom Stuart<tom at experthuman.com>
wrote:> On 21 Aug 2009, at 17:15, George Anderson wrote:
>>
>> expected: Fri, 21 Aug 2009 16:08:51 UTC +00:00,
>> ? ? ? ?got: Fri, 21 Aug 2009 16:08:51 UTC +00:00 (using ==)
>> They''re identical, no?
>
> Not necessarily -- Time instances are microsecond precision, so two of them
> can differ (in microseconds) but have the same string representation (which
> only shows seconds). See what the #usec method gives you for each of the
> times.
>
> Cheers,
> -Tom
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>