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}".tap do |msg| if @unmatching_jobs.any? msg << "\nQueued jobs:" @unmatching_jobs.each do |job| msg << "\n #{base_job_message(job)}" end end end end
failure_message_when_negated()
click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 80 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 84 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 92 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
arguments_match?(job)
click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 137 def arguments_match?(job) if @args.any? deserialized_args = deserialize_arguments(job) RSpec::Mocks::ArgumentListMatcher.new(*@args).args_match?(*deserialized_args) else true end end
base_job_message(job)
click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 126 def base_job_message(job) msg_parts = [] msg_parts << "with #{deserialize_arguments(job)}" if job[:args].any? msg_parts << "on queue #{job[:queue]}" if job[:queue] msg_parts << "at #{Time.at(job[:at])}" if job[:at] "#{job[:job].name} job".tap do |msg| msg << " #{msg_parts.join(', ')}" if msg_parts.any? end end
base_message()
click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 117 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.inspect}," 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 98 def check(jobs) @matching_jobs, @unmatching_jobs = jobs.partition do |job| if arguments_match?(job) && other_attributes_match?(job) args = deserialize_arguments(job) @block.call(*args) true else false end end @matching_jobs_count = @matching_jobs.size 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
deserialize_arguments(job)
click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 172 def deserialize_arguments(job) ::ActiveJob::Arguments.deserialize(job[:args]) rescue ::ActiveJob::DeserializationError job[:args] end
other_attributes_match?(job)
click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 146 def other_attributes_match?(job) serialized_attributes.all? { |key, value| value == job[key] } end
queue_adapter()
click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 178 def queue_adapter ::ActiveJob::Base.queue_adapter end
serialized_at()
click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 158 def serialized_at @at == :no_wait ? nil : @at.to_f end
serialized_attributes()
click to toggle source
# File lib/rspec/rails/matchers/active_job.rb, line 150 def serialized_attributes {}.tap do |attributes| attributes[:at] = serialized_at 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 162 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