How to simulate passage of time during tests?

The primary function of my rails app compares database date values to the current date. In order to test this functionality, I need to simulate time passing. I currently roll back date fields to trigger certain processes. This becomes increasingly tedious as my application becomes more complex. I would love to artificially advance ActiveRecord datetime during my test to more naturally simulate the passage of time instead of tricking every field in my database into believing it. Anyone ever done this?

Hi Peter,

The primary function of my rails app compares database date values to the current date. In order to test this functionality, I need to simulate time passing. I currently roll back date fields to trigger certain processes. This becomes increasingly tedious as my application becomes more complex. I would love to artificially advance ActiveRecord datetime during my test to more naturally simulate the passage of time instead of tricking every field in my database into believing it. Anyone ever done this?

I wrote an extension to Time to support exactly this sort of testing: http://geeksomnia.com/2007/11/07/timetravel_to

Time.travel_to(1.week.from_now) do   # mess with some objects end

I've never pluginized it, but if it meets your needs it should be pretty easy for you to integrate. :slight_smile:

~ j.

John Barnette wrote:

Hi Peter,

I wrote an extension to Time to support exactly this sort of testing: http://geeksomnia.com/2007/11/07/timetravel_to

Time.travel_to(1.week.from_now) do   # mess with some objects end

I've never pluginized it, but if it meets your needs it should be pretty easy for you to integrate. :slight_smile:

~ j.

John -

This looks awesome. I shall play around with this and let you know how it goes. Muchos Gracias!

Peter

Six months or so ago, I did it this way http://talklikeaduck.denhaven2.com/articles/2007/07/18/time-flies-while-youre-having-fun-testing

Today, I'd probably use some form of stub or mock, probably what's built into RSpec.

Rick Denatale wrote:

Six months or so ago, I did it this way http://talklikeaduck.denhaven2.com/articles/2007/07/18/time-flies-while-youre-having-fun-testing

Hey Rick:

I'd like to implement this method of yours as it looks simple and I don't know how to use R-spec with integration testing. I've never done anything with modules though. From what I've read, it seems like I should be able to call your now_as method like this:

TimeMachine.now_as(2.days.ago)

However, even with your module defined in my_test.rb, I get the error "undefined method `now_as' for MyTest::TimeMachine:Module". How are you calling your 'now_as' method? Thanks for the advice.

Peter

Nevermind. I didn't realize I needed to pass the code I want to execute in a time warp inside a block. Here is what I'm using:

TimeMachine.now_as(2.days.ago){ my code }

Works great! Thanks for sharing your module and pointing me in its direction. No more tedious tricking my db into thinking it's the past!

Today, I'd probably use some form of stub or mock, probably what's built into RSpec.

That's exactly what I do now. I used to have some nifty method to go 'back to the future', but stubbing works well now. This always tends to come up, so it's a standard part of my test suites now.