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

Nick Hoffman nick at deadorange.com
Wed Nov 5 15:16:02 EST 2008

On 2008-11-05, at 15:02, Ben Mabey wrote:
> Hey Nick,
> It is generally a bad idea to stub/mock a method on the object you  
> are verifying the behaviour of. I would recommend a state-based  
> approach of testing this method as opposed to the interaction-based  
> one you are pursuing.  The reason being is that you want to verify  
> the behaviour of the object as a whole.  How the object uses it's  
> internal methods and state is none of the code example's business.   
> Without knowing the other methods on User I don't know the best way  
> to verify the behavior.. What other methods that deal with the  
> remember functionality are part of the public API?  Assuming it is  
> an AR model and you have a 'remember_me_until' column you could do  
> something like:
> it 'should remember a user for a period of time' do
> user          = create_user
> user.remember_me_for(1.week)
> user.remember_me_until.should == 1.week.from_now.utc
> end
> Again, using the #remember_me_until method is testing the internal  
> state of the object but without knowing your other methods I don't  
> what the better options (if any) are.
> HTH,
> Ben

Hi Ben. #remember_me is used to keep a user logged in to the website  
if they ticked the "Remember me?" checkbox in the login form. It's  
only called from one location in the app:

     when valid_remember_cookie? then @current_user.refresh_token #  
keeping same expiry date
     when new_cookie_flag        then @current_user.remember_me
     else                             @current_user.forget_me

So as you suggested, my spec example should probably just be checking  
to see that the User instance's "remember_me_until" attribute is set  
to an appropriate value.


More information about the rspec-users mailing list