class RSpec::Sidekiq::Matchers::Base

@api private

Attributes

actual_jobs[R]
expected_arguments[R]
expected_count[R]
expected_options[R]
klass[R]

Public Class Methods

new() click to toggle source
# File lib/rspec/sidekiq/matchers/base.rb, line 180
def initialize
  @expected_arguments = [any_args]
  @expected_options = {}
  set_expected_count :positive, 1
end

Public Instance Methods

at(timestamp) click to toggle source
# File lib/rspec/sidekiq/matchers/base.rb, line 191
def at(timestamp)
  @expected_options["at"] = timestamp.to_time.to_i
  self
end
at_least(n) click to toggle source
# File lib/rspec/sidekiq/matchers/base.rb, line 231
def at_least(n)
  set_expected_count :at_least, n
  self
end
at_most(n) click to toggle source
# File lib/rspec/sidekiq/matchers/base.rb, line 236
def at_most(n)
  set_expected_count :at_most, n
  self
end
common_message() click to toggle source
# File lib/rspec/sidekiq/matchers/base.rb, line 296
def common_message
  "#{prefix_message} #{count_message} #{klass} #{expected_count.last == 1 ? "job" : "jobs"}"
end
count_message() click to toggle source
# File lib/rspec/sidekiq/matchers/base.rb, line 304
def count_message
  case expected_count
  in [:positive, _]
    "a"
  in [:exactly, n]
    n
  in [relativity, n]
    "#{relativity.to_s.gsub('_', ' ')} #{n}"
  end
end
description() click to toggle source
# File lib/rspec/sidekiq/matchers/base.rb, line 258
def description
  "#{common_message} with arguments #{expected_arguments}"
end
exactly(n) click to toggle source
# File lib/rspec/sidekiq/matchers/base.rb, line 226
def exactly(n)
  set_expected_count :exactly, n
  self
end
failure_message() click to toggle source
# File lib/rspec/sidekiq/matchers/base.rb, line 262
def failure_message
  message = ["expected to #{common_message}"]
  if expected_arguments
    message << "  with arguments:"
    message << "    -#{formatted(expected_arguments)}"
  end

  if expected_options.any?
    message << "  with context:"
    message << "    -#{formatted(expected_options)}"
  end

  if actual_jobs.any?
    message << "but enqueued only jobs"
    if expected_arguments
      job_messages = actual_jobs.map do |job|
        base = "  -JID:#{job.jid} with arguments:"
        base << "\n    -#{formatted(job.args)}"
        if expected_options.any?
          base << "\n   with context: #{formatted(job.context)}"
        end

        base
      end

      message << job_messages.join("\n")
    end
  else
    message << "but enqueued 0 jobs"
  end

  message.join("\n")
end
failure_message_when_negated() click to toggle source
# File lib/rspec/sidekiq/matchers/base.rb, line 315
def failure_message_when_negated
  message = ["expected not to #{common_message} but enqueued #{actual_jobs.count}"]
  message << "  arguments: #{expected_arguments}" if expected_arguments.any?
  message << "  options: #{expected_options}" if expected_options.any?
  message.join("\n")
end
formatted(thing) click to toggle source
# File lib/rspec/sidekiq/matchers/base.rb, line 322
def formatted(thing)
  RSpec::Support::ObjectFormatter.format(thing)
end
immediately() click to toggle source
# File lib/rspec/sidekiq/matchers/base.rb, line 201
def immediately
  @expected_options["at"] = nil
  self
end
in(interval) click to toggle source
# File lib/rspec/sidekiq/matchers/base.rb, line 196
def in(interval)
  @expected_options["at"] = (Time.now.to_f + interval.to_f).to_i
  self
end
normalize_arguments(args) click to toggle source
# File lib/rspec/sidekiq/matchers/base.rb, line 326
def normalize_arguments(args)
  if args.is_a?(Array)
    args.map{ |x| normalize_arguments(x) }
  elsif args.is_a?(Hash)
    args.each_with_object({}) do |(key, value), hash|
      hash[key.to_s] = normalize_arguments(value)
    end
  elsif args.is_a?(Symbol)
    args.to_s
  else
    args
  end
end
on(queue) click to toggle source
# File lib/rspec/sidekiq/matchers/base.rb, line 206
def on(queue)
  @expected_options["queue"] = queue
  self
end
once() click to toggle source
# File lib/rspec/sidekiq/matchers/base.rb, line 211
def once
  set_expected_count :exactly, 1
  self
end
prefix_message() click to toggle source
# File lib/rspec/sidekiq/matchers/base.rb, line 300
def prefix_message
  raise NotImplementedError
end
set_expected_count(relativity, n) click to toggle source
# File lib/rspec/sidekiq/matchers/base.rb, line 246
def set_expected_count(relativity, n)
  n =
    case n
    when Integer then n
    when :once   then 1
    when :twice  then 2
    when :thrice then 3
    else raise ArgumentError, "Unsupported #{n} in '#{relativity} #{n}'. Use either an Integer, :once, :twice, or :thrice."
    end
  @expected_count = [relativity, n]
end
thrice() click to toggle source
# File lib/rspec/sidekiq/matchers/base.rb, line 221
def thrice
  set_expected_count :exactly, 3
  self
end
time()
Alias for: times
times() click to toggle source
# File lib/rspec/sidekiq/matchers/base.rb, line 241
def times
  self
end
Also aliased as: time
twice() click to toggle source
# File lib/rspec/sidekiq/matchers/base.rb, line 216
def twice
  set_expected_count :exactly, 2
  self
end
with(*expected_arguments) click to toggle source
# File lib/rspec/sidekiq/matchers/base.rb, line 186
def with(*expected_arguments)
  @expected_arguments = normalize_arguments(expected_arguments)
  self
end