[LRUG] Stubbing Time.zone.now

James Adam james at lazyatom.com
Mon Aug 2 06:49:12 PDT 2010


One thing to bear in mind with Timecop, particularly as your
application grows, is remembering to add a teardown with
Timecop.return in it; if you don't, the reported duration for running
your tests/specs/whatever will be something crazy like 163526.1231
seconds.

- James

On 2 August 2010 11:31, Will Tomlins <will.tomlins at unboxedconsulting.com> wrote:
> Hi People,
>
> "Timecop is great and makes all these problems go away."
> Tom Stuart
> C 2nd Aug 2010 AD
>
> Thanks Paul and others, timecop did indeed make the problems go away.
>
> Here's what I've done:
>
> When /^At "([^\"]*)" I go to (.*)$/ do |time_string, page_name|
>   Timecop.travel(time_string) do
>     When "I go to #{page_name}"
>   end
> end
>
> So then I can call the step:
>
> When At "10-10-2010 13:00:00" I go to the home page
>
> And everything works perfectly.
>
>
> Thanks people, I'll leave you with the obligatory (terrible) Timecop quote:
>
> McComb: That's C-4. Not only will it turn this house into dust, but it will
> also separate every part of you from every other part of you.
>
> Will
>
>
>
>
>
> On 02/08/2010 10:52, Paul Bowsher wrote:
>
> Have you tried Timecop instead? http://github.com/jtrupiano/timecop
> I'm using it (not within Cucumber, mind) and it works great.
> Paul Bowsher
>
>
> On Mon, Aug 2, 2010 at 10:50 AM, Will Tomlins
> <will.tomlins at unboxedconsulting.com> wrote:
>>
>> Hi People,
>>
>> I'm having a strange issue with stubbing time and I was wondering if
>> anyone here could shed a little light on the issue.
>>
>> Firstly I am trying to stub Time.zone.now in Cucumber  # Waits while the
>> calls of "don't stub time in cucumber" die down #.  So the feature I'm
>> writing explicitly required different behaviour based on the current time,
>> and I would like the language of the feature to reflect this.
>>
>> What I have at the moment:
>>
>> When /^I time-travel to "([^\"]*)"$/ do |time_string|
>>   time = Time.zone.parse(time_string)
>>   Time.zone.stub!(:now).and_return(time)
>> end
>>
>> Then "I return to the present day" do
>>   Time.zone.unstub!(:now)
>> end
>>
>> And my features go along these lines:
>>
>>     When I time-travel to "2010/01/01 10:00:00"
>>     And I go to the home page
>>     Then I should see XXXXXXXXXXXXXXXXXXX
>>     Then I return to the present day
>>
>>
>> And I've got 2 scenarios running which use these quantum leaps.  Each
>> scenario runs and passes when run on their own, but the second will fail if
>> the two are run together.  After a bit more digging I found that
>> Time.zone.now cannot be stubbed if it has previously been unstubbed.
>>
>> Thus:
>>
>> (rdb:1) Time.zone.stub!(:now).and_return(:something)
>>
>> #<Proc:0x051b55a8@/Users/will/Sites/five-tv/vendor/gems/rspec-1.3.0/lib/spec/mocks/message_expectation.rb:61>
>> (rdb:1) Time.zone.now
>> :something
>>
>> (rdb:1) Time.zone.stub!(:now).and_return(:something_else)
>>
>> #<Proc:0x051b55a8@/Users/will/Sites/five-tv/vendor/gems/rspec-1.3.0/lib/spec/mocks/message_expectation.rb:61>
>> (rdb:1) Time.zone.now
>> :something_else
>>
>> (rdb:1) Time.zone.unstub!(:now)
>> #<Spec::Mocks::MessageExpectation:0x512c5dc @at_most=nil,
>> @exception_to_raise=nil, @args_to_yield_were_cloned=false.......
>> (rdb:1) Time.zone.now
>> Mon, 02 Aug 2010 10:28:56 BST +01:00
>>
>> (rdb:1) Time.zone.stub!(:now).and_return(:another_thing)
>>
>> #<Proc:0x051b55a8@/Users/will/Sites/five-tv/vendor/gems/rspec-1.3.0/lib/spec/mocks/message_expectation.rb:61>
>> (rdb:1) Time.zone.now
>> Mon, 02 Aug 2010 10:29:12 BST +01:00
>>
>> I have to unstub because the time stub seems to carry across scenarios.
>>
>> Has anyone else had this issue, or know of any other way to decently
>> handle present time in cucumber?
>>
>> Thanks,
>> Will
>>
>>
>>
>> _______________________________________________
>> Chat mailing list
>> Chat at lists.lrug.org
>> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>>
>
>
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>
>
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>
>



More information about the Chat mailing list