diff --git a/README.md b/README.md index 50b8ba4..0213f24 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,28 @@ AwesomeJob.perform_in 5.minutes, 'Awesome', true expect(AwesomeJob).to have_enqueued_sidekiq_job('Awesome', true).in(5.minutes) ``` +#### Testing queue set for job + +Use the chainable `#on` matcher + +```ruby +class AwesomeJob + include Sidekiq::Job + + sidekiq_options queue: :low +end + +AwesomeJob.perform_async("a little awesome") + +# test with.. +expect(AwesomeJob).to have_enqueued_sidekiq_job("a little awesome").on("low") + +# Setting the queue when enqueuing +AwesomeJob.set(queue: "high").perform_async("Very Awesome!") + +expect(AwesomeJob).to have_enqueued_sidekiq_job("Very Awesome!").on("high") +``` + #### Testing ActiveMailer jobs diff --git a/lib/rspec/sidekiq/matchers/have_enqueued_job.rb b/lib/rspec/sidekiq/matchers/have_enqueued_job.rb index 34d388e..a42e496 100644 --- a/lib/rspec/sidekiq/matchers/have_enqueued_job.rb +++ b/lib/rspec/sidekiq/matchers/have_enqueued_job.rb @@ -17,16 +17,26 @@ def initialize(job) @job = job end - def matches?(option, value) - raise ArgumentError, "Option `#{option}` is not defined." unless %w[at].include?(option.to_s) - send("#{option}_evaluator", value) + def matches?(options) + with_context(**options) end private def at_evaluator(value) - return false if job['at'].to_s.empty? - value == Time.at(job['at']).to_i + return false if job["at"].to_s.empty? + value == Time.at(job["at"]).to_i + end + + def with_context(**expected_context) + expected_context.all? do |key, value| + if key == "at" + # send to custom evaluator + at_evaluator(value) + else + job.context.has_key?(key) && job.context[key] == value + end + end end end @@ -92,7 +102,7 @@ def args end def context - @context||= job.except("args") + @context ||= job.except("args") end end @@ -121,10 +131,9 @@ def arguments_matches?(job, arguments) end def options_matches?(job, options) - options.all? do |option, value| - parser = JobOptionParser.new(job) - parser.matches?(option, value) - end + parser = JobOptionParser.new(job) + + parser.matches?(options) end def unwrap_jobs(jobs) @@ -151,12 +160,17 @@ def matches?(klass) end def at(timestamp) - @expected_options['at'] = timestamp.to_time.to_i + @expected_options["at"] = timestamp.to_time.to_i self end def in(interval) - @expected_options['at'] = (Time.now.to_f + interval.to_f).to_i + @expected_options["at"] = (Time.now.to_f + interval.to_f).to_i + self + end + + def on(queue) + @expected_options["queue"] = queue self end diff --git a/rspec-sidekiq.gemspec b/rspec-sidekiq.gemspec index 91f5533..8fb9519 100644 --- a/rspec-sidekiq.gemspec +++ b/rspec-sidekiq.gemspec @@ -11,6 +11,7 @@ Gem::Specification.new do |s| s.license = "MIT" s.add_dependency "rspec-core", "~> 3.0" + s.add_dependency "rspec-expectations", "~> 3.0" s.add_dependency "sidekiq", ">= 5", "< 8" s.add_development_dependency "pry" diff --git a/spec/rspec/sidekiq/matchers/have_enqueued_job_spec.rb b/spec/rspec/sidekiq/matchers/have_enqueued_job_spec.rb index e082bd6..8f73cfc 100644 --- a/spec/rspec/sidekiq/matchers/have_enqueued_job_spec.rb +++ b/spec/rspec/sidekiq/matchers/have_enqueued_job_spec.rb @@ -55,6 +55,20 @@ expect(worker).to have_enqueued_sidekiq_job(*worker_args_at).at(tomorrow) end end + + context "#on queue" do + it "matches the queue in the context" do + worker.perform_async(*worker_args) + expect(worker).to have_enqueued_sidekiq_job(*worker_args).on("default") + end + + context "when setting queue at runtime" do + it "matches the queue set" do + worker.set(queue: "highest").perform_async(*worker_args) + expect(worker).to have_enqueued_sidekiq_job(*worker_args).on("highest") + end + end + end end context 'ActiveJob' do