

Sometimes Rails Date/Time methods don't play nicely with Ruby Date/Time methods.īe careful mixing Ruby Date.today with Rails Date.tomorrow / Date.yesterday as things might break. freeze # => Timecop::SafeModeException: Safe mode is enabled, only calls passing a block are allowed. safe_mode = true # check if you are in safe mode If you are running in safe mode and use Timecop without the block syntax Timecop::SafeModeException will be raised to tell the user they are not being safe. Safe mode forces you to use Timecop with the block syntax since it always puts time back the way it was. See #42 for more information, thanks to Ken Mayer, David Holcomb, and Pivotal Labs. # seconds later, hours have passed and it's gone from 9pm at night to 6am in the morning Is being able to test reports and invoices that run in 30 day cycles in very little time, while alsoīeing able to simulate activity via subsequent calls to your application. In minutes while you're able to simulate "real" activity. Let's say you want to test a "live" integration wherein entire days could pass by travel ( new_time ) sleep ( 10 ) new_time = Time. freeze ( new_time ) sleep ( 10 ) new_time = Time.


TIMECOP GEM CODE
The following code snippets: new_time = Time. It uses this offset to simulate the passage of time. Time.now is (recall that we support nested traveling) and the time passed in. Travel, on the other hand, computes an offset between what we currently think Time.now will not change unless you make subsequent calls into the Timecop API. travel ( t ) end The difference between eeze and avelįreeze is used to statically mock the concept of now. after_initialize do # Set Time.now to Septem10:05:00 AM (at this instant), but allow it to move forward Your test data at a single point in time, and to move in/out of that time as Helpful if your whole application is time-sensitive. Set the time for the test environment of a rails app - this is particularly return end it " should do blah blah blah " do end end You can mock the time for a set of tests easily via setup/teardown methods describe " some set of tests to mock " do before do Timecop. mortgage_due? # move ahead a month and assert that the mortgage is due Works with regular Ruby projects, and Ruby on Rails projects.Nested calls to Timecop#travel and Timecop#freeze are supported - each block will maintain its interpretation of now.a single integer argument that is interpreted as an offset in seconds from Time.now.individual arguments (year, month, day, hour, minute, second).Timecop api allows arguments to be passed into #freeze and #travel as one of the following:.No dependencies, can be used with any ruby project.Scale time by a given scaling factor that will cause time to move at an accelerated pace.Travel back to a specific point in time, but allow time to continue moving forward from there.It provides a unified method to mock Time.now, Date.today, and DateTime.now in a single call. A gem providing "time travel" and "time freezing" capabilities, making it dead simple to test time-dependent code.
