[rspec-users] Stubbing out Time correctly

Matt Wynne matt at mattwynne.net
Mon Jul 13 14:21:40 EDT 2009


On 13 Jul 2009, at 19:17, Scott Taylor wrote:

>
> On Jul 13, 2009, at 2:15 PM, Scott Taylor wrote:
>
>>
>> On Jul 13, 2009, at 2:12 PM, David Chelimsky wrote:
>>
>>> On Mon, Jul 13, 2009 at 1:04 PM, Scott  
>>> Taylor<scott at railsnewbie.com> wrote:
>>>>
>>>> On Jul 13, 2009, at 1:46 PM, Scott Taylor wrote:
>>>>
>>>> On Jul 13, 2009, at 1:32 PM, Adam Anderson wrote:
>>>>
>>>> Thanks for the reply, Scott.
>>>>
>>>> What you describe is what is currently being done.
>>>>
>>>> now = Time.now
>>>> Time.stub!(:now).and_return(foo_time)
>>>> do_stuff
>>>> Time.stub!(:now).and_return(now)
>>>>
>>>> However, this is not returning the actual time it is only  
>>>> returning the time
>>>> set on the first line. I need Time.now to return to its original  
>>>> behavior
>>>> (i.e., returning the current, actual time).
>>>>
>>>> Well, every time you create a stub, rspec aliased the method  
>>>> before it
>>>> destroys it:
>>>> http://gist.github.com/1ed96143092a02cb727b
>>>> It's ugly, but it will work.
>>>>
>>>> Here's another solution, without needing to resort to the library:
>>>> http://gist.github.com/e573cb79073805a632c9
>>>> Of course, this solution only works if you don't have other stubs  
>>>> on Time.
>>>
>>> It also only works as long as that method keeps its name :) That
>>> method is explicitly :nodoc:'d which indicates that it is an  
>>> internal
>>> method and therefore subject to change. Not saying I plan to change
>>> it, just that of now it is not a public API.
>>>
>>> That said, this thread suggests that we need a public API for
>>> resetting partial stubs like this. I'm thinking something more
>>> intention revealing like tear_down_rspec_stubs or something like  
>>> that.
>>> Thoughts? Recommendations?
>>
>> I'd like unstub! to match stub!.
>>
>> Might also consider renaming rspec_reset, or making it part of the  
>> public api.
>
>
> BTW, I'd also be willing to contribute a patch for unstub!, if it is  
> so desired.

Given that we can stack up stubs and should_receive, is there a danger  
it could get confusing as to exactly which stub you want to unstub?

cheers,
Matt Wynne

http://mattwynne.net
+447974 430184



More information about the rspec-users mailing list