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

Viewed 24570

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
4 Answers
require 'timecop'

# freeze time to August 1st 2022 in this block
Timecop.freeze(2022, 8, 1) do
  result = something_that_uses_time()
end
Related