Disable handle_asynchronously in test mode
If you're using handle_asynchronously, I think it's reasonable to assume that you want the delaying behaviour to be "transparent" within your app. Therefore, in test mode, the relevant methods should not actually become a delayed job.
I'm achieving this currently using:
# Disable transparent delayed_job methods in test mode
module Delayed::MessageSending
def send_later(method, *args)
send(method, *args)
end
end
This has the side effect of also disabling send_later. I have mixed feelings about whether that should actually happen, as it's a more explicit call, so would be interested in thoughts on that.
What about only calling #handle_asynchronously if you're in test mode?
def do_something
# here
end
handle_asynchronously :do_something unless Rails.env.test?
Or less intrusive…make all of your #handle_asynchronously calls in config/environments/production.rb?
Hmm. The first solution isn't great because rcov will report that line as un-tested. The second - I'd prefer not as this is something which will easily confuse somebody who hasn't seen the codebase before.
I test this behaviour by ensuring that calling the method which is to be handled asynchronously returns an instance of Delayed::Backend::ActiveRecord::Job, then I test the method itself by calling invoke_job on the Delayed::Backend::ActiveRecord::Job instance
I think this is fixed by Delayed::Worker.delay_jobs = false
Was having a separate issue with my delayed jobs and got to this PR and ended up digging around in the wrong direction for a bit- probably worth closing!