Module: LittleMonster::RSpec::JobHelper

Defined in:
lib/little_monster/rspec/helpers/job_helper.rb

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Instance Method Details

#generate_job(job, options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/little_monster/rspec/helpers/job_helper.rb', line 44

def generate_job(job, options = {})
  job_class = job.class == Class ? job : job.to_s.camelcase.constantize
  job_class.mock!

  job_instance = job_class.new(data: { outputs: options.fetch(:data, {}) })
  job_instance.define_singleton_method(:is_cancelled?) { options.fetch(:cancelled, false) }

  if options[:fails]
    options[:fails] = [options[:fails]] unless options[:fails].is_a? Array
    options[:fails].each do |hash|
      allow_any_instance_of(job_class.task_class_for(hash[:task])).to receive(:run).and_raise(hash.fetch(:error, StandardError.new))
    end
  end

  job_instance
end

#run_job(job, options = {}) ⇒ Object



40
41
42
# File 'lib/little_monster/rspec/helpers/job_helper.rb', line 40

def run_job(job, options = {})
  Result.new(generate_job(job, options))
end