Skip to content

Commit

Permalink
Restore normalizing args (#205)
Browse files Browse the repository at this point in the history
* Restore normalizing args

Handles recursively stringifying symbols in expected args

* Add symbolized args spec for enqueue_sidekiq_job
  • Loading branch information
wspurgin committed Aug 23, 2023
1 parent 5998685 commit 1372f75
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
23 changes: 11 additions & 12 deletions lib/rspec/sidekiq/matchers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def initialize
end

def with(*expected_arguments)
@expected_arguments = expected_arguments
@expected_arguments = normalize_arguments(expected_arguments)
self
end

Expand Down Expand Up @@ -237,18 +237,17 @@ def formatted(thing)
RSpec::Support::ObjectFormatter.format(thing)
end

def jsonified_expected_arguments
# We would just cast-to-parse-json, but we need to support
# RSpec matcher args like #kind_of
@jsonified_expected_arguments ||= begin
expected_arguments.map do |arg|
case arg.class
when Symbol then arg.to_s
when Hash then JSON.parse(arg.to_json)
else
arg
end
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
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rspec/sidekiq/matchers/have_enqueued_sidekiq_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ def have_enqueued_sidekiq_job(*expected_arguments)
class HaveEnqueuedSidekiqJob < Base
def initialize(expected_arguments)
super()
@expected_arguments = expected_arguments
@expected_arguments = normalize_arguments(expected_arguments)
end

def matches?(job_class)
@klass = job_class

@actual_jobs = EnqueuedJobs.new(klass)

actual_jobs.includes?(jsonified_expected_arguments, expected_options)
actual_jobs.includes?(expected_arguments, expected_options)
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions spec/rspec/sidekiq/matchers/enqueue_sidekiq_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@
}.to enqueue_sidekiq_job.with("some_arg")
end

context "when expected arguments include symbols" do
it "returns true" do
expect {
worker.perform_async("foo", {"some_arg" => "etc"})
}.to enqueue_sidekiq_job.with(:foo, {some_arg: :etc})
end
end

it "fails if no job with args are found" do
expect do
expect {
Expand Down
11 changes: 11 additions & 0 deletions spec/rspec/sidekiq/matchers/have_enqueued_sidekiq_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
expect(worker).to have_enqueued_sidekiq_job(hash_including("something" => "Awesome"))
expect(worker).to have_enqueued_sidekiq_job(any_args)
expect(worker).to have_enqueued_sidekiq_job(hash_excluding("bad_stuff" => anything))

worker.perform_async({"something" => 1})
expect(worker).to have_enqueued_sidekiq_job({something: kind_of(Integer)})
end
end

Expand Down Expand Up @@ -190,6 +193,14 @@
end
end

context "when expected arguments include symbols" do
let(:worker_args) { [:foo, {bar: :baz}] }
it "returns true" do
worker.perform_async(*JSON.parse(worker_args.to_json))
expect(worker).to have_enqueued_sidekiq_job(*worker_args)
end
end

context 'when expected are matchers' do
it 'returns true' do
worker.perform_async *worker_args
Expand Down

0 comments on commit 1372f75

Please sign in to comment.