[rspec-users] Stubbing times: #from_now et al.

Nick Hoffman nick at deadorange.com
Wed Nov 5 13:51:34 EST 2008

I had a look around for how to stub Time.now , #from_now , etc, and  
came across this, which was useful:

Unfortunately, my situation is slightly different, and causes that  
solution to not be applicable. This is what I'm trying to spec:

def remember_me_for(time)
   remember_me_until time.from_now.utc

I thought this would work:

it 'should remember a user for a period of time' do
   user          = create_user
   one_week      = 1.week
   from_now      = 1.week.from_now
   from_now_utc  = 1.week.from_now.utc

   one_week.stub!(:from_now).and_return from_now
   from_now.stub!(:utc).and_return from_now_utc

   user.should_receive(:remember_me_until).with from_now_utc

   user.remember_me_for one_week

But that fails, referencing the stub on "one_week":

TypeError in 'User should remember a user for a period of time'
no virtual class for Fixnum

Any suggestions for how to solve this? Thanks!

More information about the rspec-users mailing list