Rails 4.2 & travel_to is not working

128 views
Skip to first unread message

Cezinha Anjos

unread,
Feb 17, 2015, 8:03:53 PM2/17/15
to rubyonra...@googlegroups.com
Hi!

I have two projects running Rails 4.2 and using travel_to from ActiveSupport::Testing::TimeHelpers.

In one project everything is working perfectly. But in the second one, my specs are running with the current date/time instead of using travel_to. Here is one example:

  it "is only a travel_to test" do
    travel_to Time.new(2012, 1, 1, 1, 1, 1) do
      expect(Time.new).to eq Time.new(2012, 1, 1, 1, 1, 1)
    end
  end

I've already set the rails_helper.rb with:

RSpec.configure do |config|
  ...
  config.include ActiveSupport::Testing::TimeHelpers
end

Does anyone know what's going on?

Thanks.

Colin Law

unread,
Feb 18, 2015, 4:21:02 AM2/18/15
to rubyonra...@googlegroups.com
What do Time.current and Time.now give you inside the block?

Colin

Cezinha - ASSEINFO

unread,
Feb 18, 2015, 6:04:24 AM2/18/15
to rubyonra...@googlegroups.com
Bingo!

It's returning the "travel_to" time.

Do you know why?

Thanks A LOT!!! 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvHWzjwnoEirh09pBdTfCUA64OpoPvxJYkiYzih0pHW%2BQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Colin Law

unread,
Feb 18, 2015, 6:09:36 AM2/18/15
to rubyonra...@googlegroups.com
On 18 February 2015 at 11:03, Cezinha - ASSEINFO <ce...@asseinfo.com.br> wrote:
> Bingo!
>
> It's returning the "travel_to" time.
>
> Do you know why?

Can you point to some documentation that says that Time.new()
initialises to current? I could not find any.

Colin
>> email to rubyonrails-ta...@googlegroups.com.
>> To post to this group, send email to rubyonra...@googlegroups.com.
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/CACDeCpshotmnGntZc5g51bt2g%2BKcV6r_0%3DwV%2B2vJpAiy1Yz7zQ%40mail.gmail.com.

Cezinha - ASSEINFO

unread,
Feb 18, 2015, 7:04:53 AM2/18/15
to rubyonra...@googlegroups.com
You can find here:

http://ruby-doc.org//core-2.2.0/Time.html

new → time

new(year, month=nil, day=nil, hour=nil, min=nil, sec=nil, utc_offset=nil) → time

Returns a Time object.

It is initialized to the current system time if no argument is given.

a = Time.new      #=> 2007-11-19 07:50:02 -0600

Thanks.



>> email to rubyonrails-talk+unsubscribe@googlegroups.com.
>> To post to this group, send email to rubyonrails-talk@googlegroups.com.

>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvHWzjwnoEirh09pBdTfCUA64OpoPvxJYkiYzih0pHW%2BQ%40mail.gmail.com.
>> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-talk+unsubscribe@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvSh%2BZDxH-Eq_Kd-C37EgOmDLHLq04gg-uLPf4StXQ1tg%40mail.gmail.com.

Colin Law

unread,
Feb 18, 2015, 7:23:46 AM2/18/15
to rubyonra...@googlegroups.com
On 18 February 2015 at 12:04, Cezinha - ASSEINFO <ce...@asseinfo.com.br> wrote:
> You can find here:
>
> http://ruby-doc.org//core-2.2.0/Time.html
>
> new → time
>
> new(year, month=nil, day=nil, hour=nil, min=nil, sec=nil, utc_offset=nil) →
> time
>
> Returns a Time object.
>
> It is initialized to the current system time if no argument is given.
>
> a = Time.new #=> 2007-11-19 07:50:02 -0600

Yes, you are right, and since that document also says that Time.now is
an alias for this, and the docs for travel_to states that Time.now
should get the travel_to time (have you tried Time.now in the block?)
then it looks like you have found a bug.

Colin
>> >> email to rubyonrails-ta...@googlegroups.com.
>> >> To post to this group, send email to rubyonra...@googlegroups.com.
>> >> To view this discussion on the web visit
>> >>
>> >> https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvHWzjwnoEirh09pBdTfCUA64OpoPvxJYkiYzih0pHW%2BQ%40mail.gmail.com.
>> >> For more options, visit https://groups.google.com/d/optout.
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "Ruby on Rails: Talk" group.
>> > To unsubscribe from this group and stop receiving emails from it, send
>> > an
>> > email to rubyonrails-ta...@googlegroups.com.
>> > To post to this group, send email to rubyonra...@googlegroups.com.
>> > To view this discussion on the web visit
>> >
>> > https://groups.google.com/d/msgid/rubyonrails-talk/CACDeCpshotmnGntZc5g51bt2g%2BKcV6r_0%3DwV%2B2vJpAiy1Yz7zQ%40mail.gmail.com.
>> >
>> > For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Ruby on Rails: Talk" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to rubyonrails-ta...@googlegroups.com.
>> To post to this group, send email to rubyonra...@googlegroups.com.
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/CACDeCpuQ%3DGJ18iaxQCBhO9pdAp1ZAU%3DgPEzP%2BBbtAoFjBWL98Q%40mail.gmail.com.

Hassan Schroeder

unread,
Feb 18, 2015, 8:30:57 AM2/18/15
to rubyonrails-talk
On Tue, Feb 17, 2015 at 5:03 PM, Cezinha Anjos <cezinh...@gmail.com> wrote:

> I have two projects running Rails 4.2 and using travel_to from
> ActiveSupport::Testing::TimeHelpers.

> it "is only a travel_to test" do
> travel_to Time.new(2012, 1, 1, 1, 1, 1) do
> expect(Time.new).to eq Time.new(2012, 1, 1, 1, 1, 1)
> end
> end

From the doc for `travel_to` --
Changes current time to the given time by stubbing Time.now and
Date.today to return the time or date passed into this method.

If you change your test to `expect(Time.now).to ...` it will pass.

HTH,
--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Colin Law

unread,
Feb 18, 2015, 9:03:12 AM2/18/15
to rubyonra...@googlegroups.com
On 18 February 2015 at 12:23, Colin Law <cla...@gmail.com> wrote:
> On 18 February 2015 at 12:04, Cezinha - ASSEINFO <ce...@asseinfo.com.br> wrote:
>> You can find here:
>>
>> http://ruby-doc.org//core-2.2.0/Time.html
>>
>> new → time
>>
>> new(year, month=nil, day=nil, hour=nil, min=nil, sec=nil, utc_offset=nil) →
>> time
>>
>> Returns a Time object.
>>
>> It is initialized to the current system time if no argument is given.
>>
>> a = Time.new #=> 2007-11-19 07:50:02 -0600
>
> Yes, you are right, and since that document also says that Time.now is
> an alias for this, and the docs for travel_to states that Time.now
> should get the travel_to time (have you tried Time.now in the block?)
> then it looks like you have found a bug.

I think I have to retract this, since the docs for travel_to state
that Time.now and Time.current are stubbed then it means that Time.now
is no longer an alias for Time.new(), so there is no guarantee that
Time.new() will respect travel_to.

Colin

Cezinha - ASSEINFO

unread,
Feb 18, 2015, 9:42:27 AM2/18/15
to rubyonra...@googlegroups.com
Hi!

I changed my code to use Time.now instead of Time.new and now everything is ok.

Do you know where can I check if this bugs is reported and if no... to report?

Thanks.

>>> >> email to rubyonrails-talk+unsubscribe@googlegroups.com.
>>> >> To post to this group, send email to rubyonrails-talk@googlegroups.com.

>>> >> To view this discussion on the web visit
>>> >>
>>> >> https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvHWzjwnoEirh09pBdTfCUA64OpoPvxJYkiYzih0pHW%2BQ%40mail.gmail.com.
>>> >> For more options, visit https://groups.google.com/d/optout.
>>> >
>>> > --
>>> > You received this message because you are subscribed to the Google
>>> > Groups
>>> > "Ruby on Rails: Talk" group.
>>> > To unsubscribe from this group and stop receiving emails from it, send
>>> > an
>>> > email to rubyonrails-talk+unsubscribe@googlegroups.com.
>>> > To post to this group, send email to rubyonrails-talk@googlegroups.com.

>>> > To view this discussion on the web visit
>>> >
>>> > https://groups.google.com/d/msgid/rubyonrails-talk/CACDeCpshotmnGntZc5g51bt2g%2BKcV6r_0%3DwV%2B2vJpAiy1Yz7zQ%40mail.gmail.com.
>>> >
>>> > For more options, visit https://groups.google.com/d/optout.
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Ruby on Rails: Talk" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an
>>> email to rubyonrails-talk+unsubscribe@googlegroups.com.
>>> To post to this group, send email to rubyonrails-talk@googlegroups.com.

>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvSh%2BZDxH-Eq_Kd-C37EgOmDLHLq04gg-uLPf4StXQ1tg%40mail.gmail.com.
>>> For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Ruby on Rails: Talk" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to rubyonrails-talk+unsubscribe@googlegroups.com.
>> To post to this group, send email to rubyonrails-talk@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLv48ZS1%2BQ6d6uKqNBcHq0y8jwe3cjsqqAiUCH%2BGvYv%2BhA%40mail.gmail.com.

Colin Law

unread,
Feb 18, 2015, 9:54:46 AM2/18/15
to rubyonra...@googlegroups.com
On 18 February 2015 at 14:41, Cezinha - ASSEINFO <ce...@asseinfo.com.br> wrote:
> Hi!
>
> I changed my code to use Time.now instead of Time.new and now everything is
> ok.
>
> Do you know where can I check if this bugs is reported and if no... to
> report?

Probably our posts crossed, see my last email for reason why I don't
think this is a bug.
>> >>> >> email to rubyonrails-ta...@googlegroups.com.
>> >>> >> To post to this group, send email to
>> >>> >> rubyonra...@googlegroups.com.
>> >>> >> To view this discussion on the web visit
>> >>> >>
>> >>> >>
>> >>> >> https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvHWzjwnoEirh09pBdTfCUA64OpoPvxJYkiYzih0pHW%2BQ%40mail.gmail.com.
>> >>> >> For more options, visit https://groups.google.com/d/optout.
>> >>> >
>> >>> > --
>> >>> > You received this message because you are subscribed to the Google
>> >>> > Groups
>> >>> > "Ruby on Rails: Talk" group.
>> >>> > To unsubscribe from this group and stop receiving emails from it,
>> >>> > send
>> >>> > an
>> >>> > email to rubyonrails-ta...@googlegroups.com.
>> >>> > To post to this group, send email to
>> >>> > rubyonra...@googlegroups.com.
>> >>> > To view this discussion on the web visit
>> >>> >
>> >>> >
>> >>> > https://groups.google.com/d/msgid/rubyonrails-talk/CACDeCpshotmnGntZc5g51bt2g%2BKcV6r_0%3DwV%2B2vJpAiy1Yz7zQ%40mail.gmail.com.
>> >>> >
>> >>> > For more options, visit https://groups.google.com/d/optout.
>> >>>
>> >>> --
>> >>> You received this message because you are subscribed to the Google
>> >>> Groups
>> >>> "Ruby on Rails: Talk" group.
>> >>> To unsubscribe from this group and stop receiving emails from it, send
>> >>> an
>> >>> email to rubyonrails-ta...@googlegroups.com.
>> >>> To post to this group, send email to
>> >>> rubyonra...@googlegroups.com.
>> >>> To view this discussion on the web visit
>> >>>
>> >>> https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvSh%2BZDxH-Eq_Kd-C37EgOmDLHLq04gg-uLPf4StXQ1tg%40mail.gmail.com.
>> >>> For more options, visit https://groups.google.com/d/optout.
>> >>
>> >> --
>> >> You received this message because you are subscribed to the Google
>> >> Groups
>> >> "Ruby on Rails: Talk" group.
>> >> To unsubscribe from this group and stop receiving emails from it, send
>> >> an
>> >> email to rubyonrails-ta...@googlegroups.com.
>> >> To post to this group, send email to rubyonra...@googlegroups.com.
>> >> To view this discussion on the web visit
>> >>
>> >> https://groups.google.com/d/msgid/rubyonrails-talk/CACDeCpuQ%3DGJ18iaxQCBhO9pdAp1ZAU%3DgPEzP%2BBbtAoFjBWL98Q%40mail.gmail.com.
>> >>
>> >> For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Ruby on Rails: Talk" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to rubyonrails-ta...@googlegroups.com.
>> To post to this group, send email to rubyonra...@googlegroups.com.
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/CACDeCptgOrcNqb%3D1Eoi-RUYdW_dXxm9FB9NrSZdkFgg%3DoERsnw%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages