[rspec-users] Specs of code that use Time.now

Ashley Moran work at ashleymoran.me.uk
Tue Feb 13 07:28:45 EST 2007


Just trying to spec a Rails model that defaults a valid_until date to  
this time tomorrow.  I've done something similar involving Dates, and  
you can stub the :today method to return something fixed.  But when I  
tried this with Time, I found that RSpec calls Time.now four times  
per spec.  So there's no way to spec code like 1.days.from_now .  The  
best I can come up with is this:

context "A Quote" do
   setup do
     @quote = Quote.new

   specify "should set the default valid_until date to this time  
tomorrow" do
     @quote.valid_until.should_be_between(1.days.from_now - 1.second,  
1.days.from_now + 1.second)

Is there a better way?  I'm not sure I like "dynamic" specs like  
this.  What's the recommendation for time- and date-related specs?


More information about the rspec-users mailing list