Last active
January 4, 2016 20:20
-
-
Save asok/8673692 to your computer and use it in GitHub Desktop.
Modified "have_enqueued_job" matcher to handle case when `Sidekiq::Testing` is disabled.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module RSpec | |
module Sidekiq | |
module Matchers | |
def have_enqueued_job *expected_arguments | |
HaveEnqueuedJob.new expected_arguments | |
end | |
class HaveEnqueuedJob | |
def initialize expected_arguments | |
@expected_arguments = expected_arguments | |
end | |
def description | |
"have an enqueued #{@klass} job with arguments #{@expected_arguments}" | |
end | |
def failure_message | |
"expected to have an enqueued #{@klass} job with arguments #{@expected_arguments}\n\n" + | |
"found: #{@actual}" | |
end | |
def matches? klass | |
@klass = klass | |
if ::Sidekiq::Testing.disabled? | |
matches_in_redis?(@klass) | |
else | |
matches_in_memory?(@klass) | |
end | |
end | |
def negative_failure_message | |
"expected to not have an enqueued #{@klass} job with arguments #{@expected_arguments}" | |
end | |
private | |
def matches_in_memory?(klass) | |
@actual = klass.jobs.map { |job| job["args"] } | |
@actual.any? { |arguments| Array(@expected_arguments) == arguments } | |
end | |
def matches_in_redis?(klass) | |
@actual = ::Sidekiq::Queue.all.map{|q| q.map(&:item) }.flatten | |
@actual.any? do |job| | |
job['class'] == @klass.to_s && job['args'] == Array(@expected_arguments) | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment