[rspec-users] Date comparisons

s.ross cwdinfo at gmail.com
Sun May 4 23:42:15 EDT 2008


Hi--

On May 3, 2008, at 9:17 AM, Joe Van Dyk wrote:

> I occasionally get this error:
>
> 1)
> 'A puzzle once featured, should no longer be nominated' FAILED
> expected: Sun May 04 09:10:26 -0700 2008,
>     got: Sun May 04 09:10:26 -0700 2008 (using ==)
> ./spec/models/puzzle_spec.rb:180:
>
>
>
> So, the dates looks the same to me.  Any ideas for how to debug?
>
> Joe

I monkeypatched DateTime (aieeeeee!) to get this effect. A custom  
matcher is probably a better solution. Here's the monkeypatch:

require File.dirname(__FILE__) + '/../spec_helper'

class DateTime
     def close?(other_date, difference)
         (other_date.to_time.to_i - DateTime.now.to_time.to_i).abs <  
difference
     end
end

#Delete this context and add some real ones
context "should be able to create a project viewer" do
   fixtures :project_viewers

   specify "fixtures should load two ProjectViewers" do
     pb = ProjectViewer.create(:comments => 'a  
comment', :last_viewed_at => DateTime.now)
     pb.last_viewed_at.should_be_close(DateTime.now, 100)
   end
end




More information about the rspec-users mailing list