### Install Timecop Ruby Gem Source: https://github.com/travisjeffery/timecop/blob/master/README.markdown Instructions on how to add the Timecop gem to your Ruby project using Bundler, making it available for use in your application or tests. ```Shell bundle add timecop ``` -------------------------------- ### Mock Time for a Test Suite using RSpec Hooks Source: https://github.com/travisjeffery/timecop/blob/master/README.markdown Illustrates how to set up and tear down time mocking for an entire test suite using `before` and `after` hooks, typically in RSpec. `Timecop.freeze` sets the time at the start of the suite, and `Timecop.return` resets it to the original time after all tests have run. ```Ruby describe "some set of tests to mock" do before do Timecop.freeze(Time.local(1990)) end after do Timecop.return end it "should do blah blah blah" do end end ``` -------------------------------- ### Enable and Test Timecop Safe Mode Source: https://github.com/travisjeffery/timecop/blob/master/README.markdown Demonstrates how to enable `Timecop.safe_mode` and verify its status. When enabled, `Timecop` methods must be used with a block, otherwise a `Timecop::SafeModeException` is raised, ensuring time is always returned to its original state after a time manipulation. ```Ruby # turn on safe mode Timecop.safe_mode = true # check if you are in safe mode Timecop.safe_mode? # => true # using method without block Timecop.freeze # => Timecop::SafeModeException: Safe mode is enabled, only calls passing a block are allowed. ``` -------------------------------- ### Demonstrating Timecop.freeze vs. Timecop.travel Behavior Source: https://github.com/travisjeffery/timecop/blob/master/README.markdown Compares the behavior of `Timecop.freeze` and `Timecop.travel`. `freeze` statically mocks time, so `Time.now` remains constant regardless of real time passing. `travel` sets an offset, allowing time to continue moving forward from the specified point, making `Time.now` advance relative to the set time. ```Ruby new_time = Time.local(2008, 9, 1, 12, 0, 0) Timecop.freeze(new_time) sleep(10) new_time == Time.now # ==> true ``` ```Ruby Timecop.return # "turn off" Timecop Timecop.travel(new_time) sleep(10) new_time == Time.now # ==> false ``` -------------------------------- ### Configure Rails Test Environment with Timecop.travel Source: https://github.com/travisjeffery/timecop/blob/master/README.markdown Shows how to configure a Rails application's test environment (`config/environments/test.rb`) to set a base time using `Timecop.travel` upon initialization. This is useful for building test data at a consistent point in time while allowing time to advance naturally within tests. ```Ruby config.after_initialize do # Set Time.now to September 1, 2008 10:05:00 AM (at this instant), but allow it to move forward t = Time.local(2008, 9, 1, 10, 5, 0) Timecop.travel(t) end ``` -------------------------------- ### Enable Mocking of Process.clock_gettime Source: https://github.com/travisjeffery/timecop/blob/master/README.markdown Shows how to enable Timecop's mocking of `Process.clock_gettime`. By default, Timecop does not mock this method, but it can be enabled by setting `Timecop.mock_process_clock` to `true` to ensure consistent time behavior across all time-related system calls. ```Ruby # turn on Timecop.mock_process_clock = true ``` -------------------------------- ### Accelerate Time with Timecop.scale Source: https://github.com/travisjeffery/timecop/blob/master/README.markdown Illustrates `Timecop.scale` for accelerating the passage of time. By setting a scale factor (e.g., 3600 to make seconds seem like hours), `Time.now` will advance much faster, which is useful for testing long-duration processes like daily reports in a very short period. ```Ruby # seconds will now seem like hours Timecop.scale(3600) Time.now # => 2012-09-20 21:23:25 -0500 # seconds later, hours have passed and it's gone from 9pm at night to 6am in the morning Time.now # => 2012-09-21 06:22:59 -0500 ``` -------------------------------- ### Freeze Time for a Specific Test Block in Ruby Source: https://github.com/travisjeffery/timecop/blob/master/README.markdown Demonstrates how to use `Timecop.freeze` with a block to temporarily set the current time for a specific test scenario. This allows testing time-dependent logic, such as checking a mortgage due date after a month has passed, ensuring the time returns to normal after the block. ```Ruby joe = User.find(1) joe.purchase_home() assert !joe.mortgage_due? # move ahead a month and assert that the mortgage is due Timecop.freeze(Date.today + 30) do assert joe.mortgage_due? end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.