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
My usage of timecop in specs always looks like this:
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.