timecop
timecop copied to clipboard
How to work with whenever gem?
Whenever is a gem for running scheduled jobs.
It supports definitions of jobs like this:
every 1.day, :at => '4:30 am' do
runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
end
When I use Timecop.travel 1.day in a test, I would expect scheduled jobs to run. (not the one definned in crontab after running wheneverize, just the ruby code in schedule.rb)
What solution do you propose?
whenever works simply by acting as a ruby DSL to generate a crontab.
- The code above does not "run" as you describe. That's simply not how it works. Only the
crontabis run. - In order to test the code in the manner you describe, you'd need to (1) generate the crontab, and then (2) update the system clock to be at a new time (e.g. 1 day ahead).
- It would only make sense to perform such a test at an end-to-end level, regardless. This is not something you should be doing with
Timecop. (Or, quite likely, at all. Why not just explicitly re-triggerMyModel.task_to_run_at_four_thirty_in_the_morningwithin the test?)