class RSpec::Rails::Matchers::ActiveJob::HaveEnqueuedJob

@private

Public Class Methods

new(job) click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 13
def initialize(job)
  @job = job
  @args = []
  @queue = nil
  @at = nil
  set_expected_number(:exactly, 1)
end

Public Instance Methods

at(date) click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 49
def at(date)
  @at = date
  self
end
at_least(count) click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 59
def at_least(count)
  set_expected_number(:at_least, count)
  self
end
at_most(count) click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 64
def at_most(count)
  set_expected_number(:at_most, count)
  self
end
exactly(count) click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 54
def exactly(count)
  set_expected_number(:exactly, count)
  self
end
failure_message() click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 73
def failure_message
  "expected to enqueue #{base_message}"
end
failure_message_when_negated() click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 77
def failure_message_when_negated
  "expected not to enqueue #{base_message}"
end
matches?(proc) click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 21
def matches?(proc)
  raise ArgumentError, "have_enqueued_jobs only supports block expectations" unless Proc === proc

  original_enqueued_jobs_count = queue_adapter.enqueued_jobs.count
  proc.call
  in_block_jobs = queue_adapter.enqueued_jobs.drop(original_enqueued_jobs_count)

  @matching_jobs_count = in_block_jobs.count do |job|
    serialized_attributes.all? { |key, value| value == job[key] }
  end

  case @expectation_type
  when :exactly then @expected_number == @matching_jobs_count
  when :at_most then @expected_number >= @matching_jobs_count
  when :at_least then @expected_number <= @matching_jobs_count
  end
end
message_expectation_modifier() click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 81
def message_expectation_modifier
  case @expectation_type
  when :exactly then "exactly"
  when :at_most then "at most"
  when :at_least then "at least"
  end
end
on_queue(queue) click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 44
def on_queue(queue)
  @queue = queue
  self
end
supports_block_expectations?() click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 89
def supports_block_expectations?
  true
end
times() click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 69
def times
  self
end
with(*args) click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 39
def with(*args)
  @args = args
  self
end

Private Instance Methods

base_message() click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 95
def base_message
  "#{message_expectation_modifier} #{@expected_number} jobs,".tap do |msg|
    msg << " with #{@args}," if @args.any?
    msg << " on queue #{@queue}," if @queue
    msg << " at #{@at}," if @at
    msg << " but enqueued #{@matching_jobs_count}"
  end
end
queue_adapter() click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 123
def queue_adapter
  ::ActiveJob::Base.queue_adapter
end
serialized_attributes() click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 104
def serialized_attributes
  {}.tap do |attributes|
    attributes[:args]  = ::ActiveJob::Arguments.serialize(@args) if @args.any?
    attributes[:at]    = @at.to_f if @at
    attributes[:queue] = @queue if @queue
    attributes[:job]   = @job if @job
  end
end
set_expected_number(relativity, count) click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 113
def set_expected_number(relativity, count)
  @expectation_type = relativity
  @expected_number = case count
                     when :once then 1
                     when :twice then 2
                     when :thrice then 3
                     else Integer(count)
                     end
end