class RSpec::Rails::Matchers::ActiveJob::Base
rubocop: disable Style/ClassLength @private
Public Class Methods
new()
click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 14 def initialize @args = [] @queue = nil @at = nil @block = Proc.new {} set_expected_number(:exactly, 1) end
Public Instance Methods
at(date)
click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 33 def at(date) @at = date self end
at_least(count)
click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 43 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 48 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 38 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 69 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 73 def failure_message_when_negated "expected not to enqueue #{base_message}" end
message_expectation_modifier()
click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 77 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 28 def on_queue(queue) @queue = queue self end
once()
click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 57 def once exactly(:once) end
supports_block_expectations?()
click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 85 def supports_block_expectations? true end
thrice()
click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 65 def thrice exactly(:thrice) end
times()
click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 53 def times self end
twice()
click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 61 def twice exactly(:twice) end
with(*args, &block)
click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 22 def with(*args, &block) @args = args @block = block if block.present? self end
Private Instance Methods
base_message()
click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 109 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
check(jobs)
click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 91 def check(jobs) @matching_jobs_count = jobs.count do |job| if serialized_attributes.all? { |key, value| value == job[key] } args = ::ActiveJob::Arguments.deserialize(job[:args]) @block.call(*args) true else false end 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
queue_adapter()
click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 137 def queue_adapter ::ActiveJob::Base.queue_adapter end
serialized_attributes()
click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 118 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 127 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