Include DelayedJobHelper for specs

This commit is contained in:
Rohan Mitchell
2015-04-10 15:42:02 +10:00
parent 3bbf42c3e7
commit af4baabb50
2 changed files with 50 additions and 47 deletions

View File

@@ -93,6 +93,7 @@ RSpec.configure do |config|
config.include OpenFoodNetwork::EnterpriseGroupsHelper
config.include OpenFoodNetwork::DistributionHelper
config.include ActionView::Helpers::DateHelper
config.include OpenFoodNetwork::DelayedJobHelper
# FactoryGirl
require 'factory_girl_rails'

View File

@@ -1,59 +1,61 @@
module DelayedJobSupport
# Process all pending Delayed jobs, keeping in mind jobs could spawn new
# delayed job (so things might be added to the queue while processing)
def flush_jobs(options = {})
options[:ignore_exceptions] ||= false
module OpenFoodNetwork
module DelayedJobHelper
# Process all pending Delayed jobs, keeping in mind jobs could spawn new
# delayed job (so things might be added to the queue while processing)
def flush_jobs(options = {})
options[:ignore_exceptions] ||= false
Delayed::Worker.new.work_off(100)
Delayed::Worker.new.work_off(100)
unless options[:ignore_exceptions]
Delayed::Job.all.each do |job|
if job.last_error.present?
throw "There was an error in a delayed job: #{job.last_error}"
end
end
end
end
def clear_jobs
Delayed::Job.delete_all
end
# expect { foo }.to enqueue_job MyJob, field1: 'foo', field2: 'bar'
RSpec::Matchers.define :enqueue_job do |klass, options = {}|
match do |event_proc|
last_job_id_before = Delayed::Job.last.andand.id || 0
event_proc.call
@jobs_created = Delayed::Job.where('id > ?', last_job_id_before)
@jobs_created.any? do |job|
job = job.payload_object
match = true
match &= (job.class == klass)
options.each_pair do |k, v|
begin
match &= (job[k] == v)
rescue NameError
match = false
unless options[:ignore_exceptions]
Delayed::Job.all.each do |job|
if job.last_error.present?
throw "There was an error in a delayed job: #{job.last_error}"
end
end
match
end
end
failure_message_for_should do |event_proc|
"expected job to be enqueued matching #{options.inspect} (#{@jobs_created.andand.count || '???'} others enqueued)"
def clear_jobs
Delayed::Job.delete_all
end
failure_message_for_should_not do |event_proc|
"expected job to not be enqueued matching #{options.inspect}"
# expect { foo }.to enqueue_job MyJob, field1: 'foo', field2: 'bar'
RSpec::Matchers.define :enqueue_job do |klass, options = {}|
match do |event_proc|
last_job_id_before = Delayed::Job.last.andand.id || 0
event_proc.call
@jobs_created = Delayed::Job.where('id > ?', last_job_id_before)
@jobs_created.any? do |job|
job = job.payload_object
match = true
match &= (job.class == klass)
options.each_pair do |k, v|
begin
match &= (job[k] == v)
rescue NameError
match = false
end
end
match
end
end
failure_message_for_should do |event_proc|
"expected job to be enqueued matching #{options.inspect} (#{@jobs_created.andand.count || '???'} others enqueued)"
end
failure_message_for_should_not do |event_proc|
"expected job to not be enqueued matching #{options.inspect}"
end
end
end
end