class RSpec::Sidekiq::Matchers::BeDelayed
Public Class Methods
Source
# File lib/rspec/sidekiq/matchers/be_delayed.rb, line 13 def initialize(*expected_arguments) raise <<~MSG if RSpec::Sidekiq.configuration.sidekiq_gte_7? Use of the be_delayed matcher with Sidekiq 7+ is not possible. Try refactoring to a Sidekiq Job with `perform_at` or `perform_in` and the `have_enqueued_sidekiq_job` matcher MSG @expected_arguments = expected_arguments end
Public Instance Methods
Source
# File lib/rspec/sidekiq/matchers/be_delayed.rb, line 21 def description description = 'be delayed' description += " for #{@expected_interval} seconds" if @expected_interval description += " until #{@expected_time}" if @expected_time description += " with arguments #{@expected_arguments}" unless @expected_arguments.empty? description end
Source
# File lib/rspec/sidekiq/matchers/be_delayed.rb, line 29 def failure_message "expected #{@expected_method_receiver}.#{@expected_method.name} to " + description end
Source
# File lib/rspec/sidekiq/matchers/be_delayed.rb, line 56 def failure_message_when_negated "expected #{@expected_method_receiver}.#{@expected_method.name} to not " + description end
Source
# File lib/rspec/sidekiq/matchers/be_delayed.rb, line 33 def for(interval) @expected_interval = interval self end
Source
# File lib/rspec/sidekiq/matchers/be_delayed.rb, line 38 def matches?(expected_method) @expected_method = expected_method @expected_method_receiver = @expected_method.is_a?(UnboundMethod) ? @expected_method.owner : @expected_method.receiver find_job @expected_method, @expected_arguments do |job| if @expected_interval created_enqueued_at = job['enqueued_at'] || job['created_at'] return job['at'].to_i == Time.at(created_enqueued_at.to_f + @expected_interval.to_f).to_i elsif @expected_time return job['at'].to_i == @expected_time.to_i else return true end end false end
Source
# File lib/rspec/sidekiq/matchers/be_delayed.rb, line 60 def until(time) @expected_time = time self end
Private Instance Methods
Source
# File lib/rspec/sidekiq/matchers/be_delayed.rb, line 67 def find_job(method, arguments, &block) job = (::Sidekiq::Extensions::DelayedClass.jobs + ::Sidekiq::Extensions::DelayedModel.jobs + ::Sidekiq::Extensions::DelayedMailer.jobs).find do |job| arg = job['args'].first yaml = begin YAML.load(arg, aliases: true) # Psych 4 required syntax rescue ArgumentError YAML.load(arg) # Pysch < 4 syntax end @expected_method_receiver == yaml[0] && method.name == yaml[1] && (arguments.empty? || RSpec::Mocks::ArgumentListMatcher.new(*arguments).args_match?(*yaml[2])) end yield job if block && job end