Score:0

How can I test code that uses cron for scheduling?

cn flag

I have some custom code on my site that schedules emails to be sent. When an email is set, a user account field is updated. So I want to write a test that:

  1. Jumps forward in time into the future
  2. Runs cron
  3. Checks the value of the email field
  4. Jumps forward in time into the future (and repeats 1-3)

Generally, to advance time in my tests, I'm using the Datetime Testing module, but this doesn't work for adjusting cron (cron always gets executed at the present time).

How can I spoof the time when cron runs so that I am executing cron as if it was in the future?

Kevin avatar
in flag
Could you do the internal logic as a service and test that instead? Is cron checking time as now()? Can you mock the DateTime service to return mocked date/time for your service?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.