Timecop is maintained by travisjeffery, and 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. 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
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |