Skip to content

Commit

Permalink
Merge pull request #197 from wspurgin/with_context
Browse files Browse the repository at this point in the history
Add `#on` matcher
  • Loading branch information
wspurgin authored Jul 31, 2023
2 parents 577e028 + ed22bbc commit 984baeb
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
38 changes: 26 additions & 12 deletions lib/rspec/sidekiq/matchers/have_enqueued_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -92,7 +102,7 @@ def args
end

def context
@context||= job.except("args")
@context ||= job.except("args")
end
end

Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions rspec-sidekiq.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions spec/rspec/sidekiq/matchers/have_enqueued_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 984baeb

Please sign in to comment.