Hi 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 end 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) end end 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? Ashley
On 2/13/07, Ashley Moran <work at ashleymoran.me.uk> wrote:> 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.Personally I like to put this in spec_helper def Time.now Time.local(2006,12,01,0,0,0) end Then all dates are fixed (1 day from now is 2006-12-02 for example). No clue if this is best practise, works well here though.
On 13 Feb 2007, at 16:15, Courtenay wrote:> Personally I like to put this in spec_helper > > def Time.now > Time.local(2006,12,01,0,0,0) > end > > Then all dates are fixed (1 day from now is 2006-12-02 for example). > No clue if this is best practise, works well here though.Does this not interfere with RSpec? I noticed it calling Time.now four times without any code on my part. I suppose something has to give though Ashley
On 2/13/07, Ashley Moran <work at ashleymoran.me.uk> wrote:> > def Time.now > > Time.local(2006,12,01,0,0,0) > > end > > > > Then all dates are fixed (1 day from now is 2006-12-02 for example). > > No clue if this is best practise, works well here though. > > Does this not interfere with RSpec? I noticed it calling Time.now > four times without any code on my part. I suppose something has to > give though >Dunno :) May be it''s using time.now or benchmark to time things.
On 2/13/07, Ashley Moran <work at ashleymoran.me.uk> wrote:> On 13 Feb 2007, at 16:15, Courtenay wrote: > > > Personally I like to put this in spec_helper > > > > def Time.now > > Time.local(2006,12,01,0,0,0) > > end > > > > Then all dates are fixed (1 day from now is 2006-12-02 for example). > > No clue if this is best practise, works well here though. > > Does this not interfere with RSpec? I noticed it calling Time.now > four times without any code on my part. I suppose something has to > give though >Try this: class Foo class Time < ::Time def self.now Time.local(2006,10,1,0,0,0) end end def test_local Time.now.inspect end end f = Foo.new f.test_local => "Sun Oct 01 00:00:00 -0700 2006" Time.new => Tue Feb 13 10:14:03 -0800 2007 So, just make a new subclass of your model called Time that inherits from ::Time but overrides the .now to fix the date, and it shouldn''t be that much of a problem :)
Why not try this in the applicable spec: the_time = Time.local(2006,12,01,0,0,0) Time.should_receive(:now).and_return(the_time) Will this break other stuff? On Feb 13, 2007, at 10:22 AM, Courtenay wrote:> On 2/13/07, Ashley Moran <work at ashleymoran.me.uk> wrote: >> On 13 Feb 2007, at 16:15, Courtenay wrote: >> >>> Personally I like to put this in spec_helper >>> >>> def Time.now >>> Time.local(2006,12,01,0,0,0) >>> end >>> >>> Then all dates are fixed (1 day from now is 2006-12-02 for example). >>> No clue if this is best practise, works well here though. >> >> Does this not interfere with RSpec? I noticed it calling Time.now >> four times without any code on my part. I suppose something has to >> give though >> > > Try this: > > class Foo > class Time < ::Time > def self.now > Time.local(2006,10,1,0,0,0) > end > end > > def test_local > Time.now.inspect > end > end > > f = Foo.new > f.test_local > => "Sun Oct 01 00:00:00 -0700 2006" > Time.new > => Tue Feb 13 10:14:03 -0800 2007 > > So, just make a new subclass of your model called Time that inherits > from ::Time but overrides the .now to fix the date, and it shouldn''t > be that much of a problem :) > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
On 2/13/07, Courtenay <court3nay at gmail.com> wrote:> On 2/13/07, Ashley Moran <work at ashleymoran.me.uk> wrote: > > 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. > > Personally I like to put this in spec_helper > > def Time.now > Time.local(2006,12,01,0,0,0) > end > > Then all dates are fixed (1 day from now is 2006-12-02 for example). > No clue if this is best practise, works well here though.There are no best practices. But this seems like a good practice.> _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
On 2/13/07, aslak hellesoy <aslak.hellesoy at gmail.com> wrote:> On 2/13/07, Courtenay <court3nay at gmail.com> wrote: > > On 2/13/07, Ashley Moran <work at ashleymoran.me.uk> wrote: > > > 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. > > > > Personally I like to put this in spec_helper > > > > def Time.now > > Time.local(2006,12,01,0,0,0) > > end > > > > Then all dates are fixed (1 day from now is 2006-12-02 for example). > > No clue if this is best practise, works well here though. > > There are no best practices. > But this seems like a good practice. >Time.stub! and only use should_receive when you''re specifically testing that Time.now is called.