[rspec-users] Stubbing out Time correctly

David Chelimsky dchelimsky at gmail.com
Wed Jul 15 00:15:39 EDT 2009


On Mon, Jul 13, 2009 at 1:17 PM, Scott Taylor<scott at railsnewbie.com> 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.

FYI - Scott submitted this patch and I just merged it, so you should
now be able to unstub! Time.now mid-example.

http://github.com/dchelimsky/rspec/commit/281dc54d4556fed35b62e2f1b86a13e443dcd6e8

Cheers (and thanks, Scott!),
David


>
> Scott
>
>
>>
>> Scott
>>
>>
>>>
>>>> Scott
>>>>
>>>>
>>>> You might also want to check out this library:
>>>> http://github.com/notahat/time_travel/tree/master
>>>> Scott
>>>>
>>>>
>>>> -Adam
>>>>
>>>> On Mon, Jul 13, 2009 at 9:58 AM, Scott Taylor <scott at railsnewbie.com>
>>>> wrote:
>>>>>
>>>>> On Jul 13, 2009, at 12:09 PM, Adam Anderson wrote:
>>>>>
>>>>>> I can't seem to find a good way to do this. If I stub out Time.now in
>>>>>> one
>>>>>> of my specs but need to return it to its original functionality then
>>>>>> can I
>>>>>> remove the stub?
>>>>>>
>>>>>> So I'd like to say something like:
>>>>>> Time.stub!(:now).and_return(foo_time)
>>>>>> Time.now # => foo_time
>>>>>> Time.unstub!(:now)
>>>>>> Time.now # => whatever time it actually is
>>>>>
>>>>>
>>>>> Capture it before you stub it:
>>>>>
>>>>> real_now= Time.now
>>>>>
>>>>> Time.stub!(:now).and_return a_later_time
>>>>>
>>>>> Time.stub!(:now).and_return real_now
>>>>>
>>>>>>
>>>>>> Is this possible? I noticed $rspec_mocks.reset_all, but obviously I
>>>>>> don't
>>>>>> want everything to go away, just this one stub.
>>>>>>
>>>>>>
>>>>>> -Adam
>>>>>> _______________________________________________
>>>>>> rspec-users mailing list
>>>>>> rspec-users at rubyforge.org
>>>>>> http://rubyforge.org/mailman/listinfo/rspec-users
>>>>>
>>>>> _______________________________________________
>>>>> rspec-users mailing list
>>>>> rspec-users at rubyforge.org
>>>>> http://rubyforge.org/mailman/listinfo/rspec-users
>>>>
>>>> _______________________________________________
>>>> rspec-users mailing list
>>>> rspec-users at rubyforge.org
>>>> http://rubyforge.org/mailman/listinfo/rspec-users
>>>>
>>>> _______________________________________________
>>>> rspec-users mailing list
>>>> rspec-users at rubyforge.org
>>>> http://rubyforge.org/mailman/listinfo/rspec-users
>>>>
>>>> _______________________________________________
>>>> rspec-users mailing list
>>>> rspec-users at rubyforge.org
>>>> http://rubyforge.org/mailman/listinfo/rspec-users
>>>>
>>> _______________________________________________
>>> rspec-users mailing list
>>> rspec-users at rubyforge.org
>>> http://rubyforge.org/mailman/listinfo/rspec-users
>>
>> _______________________________________________
>> rspec-users mailing list
>> rspec-users at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/rspec-users
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list