Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fake Time.now?

What's the best way to set Time.now for the purpose of testing time-sensitive methods in a unit test?

like image 993
user147962 Avatar asked Jul 31 '09 22:07

user147962


4 Answers

I really like the Timecop library. You can do time warps in block form (just like time-warp):

Timecop.travel(6.days.ago) do
  @model = TimeSensitiveMode.new
end
assert @model.times_up!

(Yes, you can nest block-form time travel.)

You can also do declarative time travel:

class MyTest < Test::Unit::TestCase
  def setup
    Timecop.travel(...)
  end
  def teardown
    Timecop.return
  end
end

I have some cucumber helpers for Timecop here. They let you do things like:

Given it is currently January 24, 2008
And I go to the new post page
And I fill in "title" with "An old post"
And I fill in "body" with "..."
And I press "Submit"
And we jump in our Delorean and return to the present
When I go to the home page
I should not see "An old post"
like image 190
James A. Rosen Avatar answered Oct 20 '22 03:10

James A. Rosen


Personally I prefer to make the clock injectable, like so:

def hello(clock=Time)
  puts "the time is now: #{clock.now}"
end

Or:

class MyClass
  attr_writer :clock

  def initialize
    @clock = Time
  end

  def hello
    puts "the time is now: #{@clock.now}"
  end
end

However, many prefer to use a mocking/stubbing library. In RSpec/flexmock you can use:

Time.stub!(:now).and_return(Time.mktime(1970,1,1))

Or in Mocha:

Time.stubs(:now).returns(Time.mktime(1970,1,1))
like image 31
Avdi Avatar answered Oct 20 '22 05:10

Avdi


I'm using RSpec and I did this: Time.stub!(:now).and_return(2.days.ago) before I call Time.now. In that way I'm able to control the time I used for that particular test case

like image 14
Staelen Avatar answered Oct 20 '22 04:10

Staelen


Using Rspec 3.2, the only simple way I found to fake Time.now return value is :

now = Time.parse("1969-07-20 20:17:40")
allow(Time).to receive(:now) { now }

Now Time.now will always return the date of Apollo 11 landing on the moon.

Source: https://www.relishapp.com/rspec/rspec-mocks/docs

like image 12
Iwazaru Avatar answered Oct 20 '22 04:10

Iwazaru