DVG
DVG

Reputation: 17480

Unexpected Rspec Results using Timecop

Here is the basic idea of the spec:

before :each do
  Timecop.freeze(Time.local(2012, 07, 01, 12, 0, 0)) #frozen at July 1st, 2012 at noon
  #create record code
end
it 'shows how long ago the message was recieved' do
  Timecop.travel(Time.local(2012, 07, 02, 12, 0, 0)) #move to July 2nd
  page.should have_content "1 day ago"
end
after :each do
  Timecop.return #release freeze
end

It errors out with:

expected there to be content "1 day ago" in "less than a minute ago"

I'm displaying <%= "#{time_ago_in_words(m.created_at)} ago" %> which I would expect to be exactly 24 hours different. What am I missing?

Upvotes: 0

Views: 1390

Answers (1)

DVG
DVG

Reputation: 17480

The problem was misunderstanding then purpose of the travel method versus the freeze method.

Freeze stops time at the selected moment, Travel sets time to the selected moment but it travels freely from there.

I solved the problem by replacing Travel with Freeze in the example.

Upvotes: 0

Related Questions