class RSpec::Sidekiq::Matchers::Base
@api private
Attributes
Public Class Methods
Source
# File lib/rspec/sidekiq/matchers/base.rb, line 186 def initialize @expected_arguments = [any_args] @expected_options = {} set_expected_count :positive, 1 end
Public Instance Methods
Source
# File lib/rspec/sidekiq/matchers/base.rb, line 197 def at(timestamp) @expected_options["at"] = timestamp.to_time.to_i self end
Source
# File lib/rspec/sidekiq/matchers/base.rb, line 237 def at_least(n) set_expected_count :at_least, n self end
Source
# File lib/rspec/sidekiq/matchers/base.rb, line 242 def at_most(n) set_expected_count :at_most, n self end
Source
# File lib/rspec/sidekiq/matchers/base.rb, line 322 def common_message "#{prefix_message} #{count_message} #{klass} #{expected_count.last == 1 ? "job" : "jobs"}" end
Source
# File lib/rspec/sidekiq/matchers/base.rb, line 330 def count_message case expected_count[0] when :positive "a" when :exactly expected_count[1] else "#{expected_count[0].to_s.gsub('_', ' ')} #{expected_count[1]}" end end
Source
# File lib/rspec/sidekiq/matchers/base.rb, line 284 def description "#{common_message} with arguments #{expected_arguments}" end
Source
# File lib/rspec/sidekiq/matchers/base.rb, line 232 def exactly(n) set_expected_count :exactly, n self end
Source
# File lib/rspec/sidekiq/matchers/base.rb, line 288 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 << " -#{formatted(job.args)}" if expected_options.any? base << " with context: #{formatted(job.context)}" end base.join("\n") end message << job_messages.join("\n") end else message << "but enqueued 0 jobs" end message.join("\n") end
Source
# File lib/rspec/sidekiq/matchers/base.rb, line 341 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
Source
# File lib/rspec/sidekiq/matchers/base.rb, line 348 def formatted(thing) RSpec::Support::ObjectFormatter.format(thing) end
Source
# File lib/rspec/sidekiq/matchers/base.rb, line 207 def immediately @expected_options["at"] = nil self end
Source
# File lib/rspec/sidekiq/matchers/base.rb, line 202 def in(interval) @expected_options["at"] = (Time.now.to_f + interval.to_f).to_i self end
Source
# File lib/rspec/sidekiq/matchers/base.rb, line 352 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
Source
# File lib/rspec/sidekiq/matchers/base.rb, line 212 def on(queue) @expected_options["queue"] = queue self end
Source
# File lib/rspec/sidekiq/matchers/base.rb, line 217 def once set_expected_count :exactly, 1 self end
Source
# File lib/rspec/sidekiq/matchers/base.rb, line 326 def prefix_message raise NotImplementedError end
Source
# File lib/rspec/sidekiq/matchers/base.rb, line 272 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
Source
# File lib/rspec/sidekiq/matchers/base.rb, line 227 def thrice set_expected_count :exactly, 3 self end
Source
# File lib/rspec/sidekiq/matchers/base.rb, line 222 def twice set_expected_count :exactly, 2 self end
Source
# File lib/rspec/sidekiq/matchers/base.rb, line 192 def with(*expected_arguments) @expected_arguments = normalize_arguments(expected_arguments) self end
Source
# File lib/rspec/sidekiq/matchers/base.rb, line 252 def with_context(**kwargs) raise ArgumentError, "Must specify keyword arguments to with_context" if kwargs.empty? # gather keys and compare against currently set expected_options # Someone could have accidentally used with_context and other # chainables with different expectations. Better to explicitly # inform loudly of clashes than let them overwrite silently normalized = normalize_arguments(kwargs) already_set = normalized.keys & @expected_options.keys if already_set.any? prettied = already_set.map { |key| "'#{key}'" } raise ArgumentError, "There are already expectations against #{prettied.join(",")}. Did you already call other context chainables like `on` or `at`?" end # We're good, no accidental overwrites of expectations @expected_options.merge!(normalized) self end