How do I correctly freeze time w/ Timecop in my spec?

21,841

Solution 1

My usage of timecop in specs always looks like this:

Timecop.travel(Time.zone.local(2010, 6, 1, 13, 0, 0)) do
  .. time sensitive spec here ..
end

It's also generally good practice to use the Time.zone proxy (Time.zone.now, Time.zone.utc, Time.zone.local, etc) when dealing with time in a rails app.

Solution 2

I just had a problem running Timecop with RSpec's expect syntax when I was running:

it "updates :completed_at" do
  Timecop.freeze
  expect(@task.completed_at).to eq(Time.zone.now)
end

Where the times weren't matching. To solve, I put Timecop.freeze in a before clause.

(I realize this question is older and RSpec's expect syntax wasn't around, but I think adding Timecop.freeze to a before block or clause may help people who have the same problem mentioned in the original question. Certainly, it doesn't seem like asking a new question and answering it is worthwhile since my question would be very very similar to the one above.)

Solution 3

Travel to the date and include TimeHelpers, example:

include ActiveSupport::Testing::TimeHelpers

let!(:archived_date) { Time.zone.now }

travel_to(archived_date) do
  expect(OrderService.getOrder(some_old_order).archived_at).to eq Time.zone.now
end
Share:
21,841
Eric M.
Author by

Eric M.

Updated on July 09, 2022

Comments

  • Eric M.
    Eric M. almost 2 years

    I am trying to use a combination of Timecop and querying the arel where_sql to data, but I can't seem to get Timecop to actually freeze the time. I've tried Timecop.freeze and Timecop.freeze(Time.now), both of which are slightly off when using Time.now in my spec.

    What am I missing? Ruby 1.9.2, Rails 3.1.0.rc5

    --

    error

    Failure/Error: Game.unreleased.arel.where_sql.should eq("WHERE (release_date > '#{Time.now}')")
    
    
         expected "WHERE (release_date > '0000-01-01 00:00:00 -0500')"
         got "WHERE (release_date > '0000-01-01 05:00:00.000000')"
    

    model

    scope :unreleased, lambda { |limit = 4| where('release_date > ?', Time.now).
                                            order('release_date asc').
                                            limit(limit) }
    

    spec

    it "should retrieve games with a release date later than today" do
      Timecop.freeze
      Game.unreleased.arel.where_sql.should eq("WHERE (release_date > '#{Time.now}')")
    end
    
  • Eric M.
    Eric M. almost 13 years
    There is still an issue with displaying the correct timestamp format as used in the where clause, but your Timecop answer is correct.
  • einjohn
    einjohn over 6 years
    You're making a good point, but you're not directly answering the question; therefore, you should have been more explicit: You're not using Timecop but a Rails Helper Method (travel_to) and this method (amongst others) is available as of Rails 4.1.
  • Andrew
    Andrew about 3 years