Skip to content

Commit

Permalink
Merge pull request #200 from wspurgin/argument-matching-order-matters
Browse files Browse the repository at this point in the history
Use ArgumentListMatcher to ensure order and allow use of richer builtin Argument matchers
  • Loading branch information
wspurgin committed Aug 1, 2023
2 parents 478b509 + 9bd2dec commit 8663f9a
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 6 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ AwesomeJob.perform_async 'Awesome', true
expect(AwesomeJob).to have_enqueued_sidekiq_job('Awesome', true)
```

You can use the built-in args matchers too:
```ruby
AwesomeJob.perform_async({"something" => "Awesome", "extra" => "stuff"})

# using built-in matchers from rspec-mocks:
expect(AwesomeJob).to have_enqueued_sidekiq_job(hash_including("something" => "Awesome"))
expect(AwesomeJob).to have_enqueued_sidekiq_job(any_args)
expect(AwesomeJob).to have_enqueued_sidekiq_job(hash_excluding("bad_stuff" => anything))
```

#### Testing scheduled jobs

*Use chainable matchers `#at` and `#in`*
Expand Down
1 change: 1 addition & 0 deletions lib/rspec/sidekiq/matchers.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'rspec/core'
require 'rspec/mocks/argument_list_matcher'
require 'rspec/sidekiq/matchers/be_delayed'
require 'rspec/sidekiq/matchers/be_expired_in'
require 'rspec/sidekiq/matchers/be_processed_in'
Expand Down
4 changes: 3 additions & 1 deletion lib/rspec/sidekiq/matchers/be_delayed.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module RSpec
module Sidekiq
module Matchers
include RSpec::Mocks::ArgumentMatchers

def be_delayed(*expected_arguments)
BeDelayed.new(*expected_arguments)
end
Expand Down Expand Up @@ -71,7 +73,7 @@ def find_job(method, arguments, &block)

@expected_method_receiver == yaml[0] &&
method.name == yaml[1] &&
(arguments.empty? || (arguments <=> yaml[2]) == 0)
(arguments.empty? || RSpec::Mocks::ArgumentListMatcher.new(*arguments).args_match?(*yaml[2]))
end

yield job if block && job
Expand Down
6 changes: 5 additions & 1 deletion lib/rspec/sidekiq/matchers/have_enqueued_sidekiq_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ def with_context(**expected_context)
end

class JobArguments
include RSpec::Mocks::ArgumentMatchers

def initialize(job)
self.job = job
end
attr_accessor :job

def matches?(expected_args)
RSpec::Matchers::BuiltIn::ContainExactly.new(expected_args).matches?(unwrapped_arguments)
matcher = RSpec::Mocks::ArgumentListMatcher.new(*expected_args)

matcher.args_match?(*unwrapped_arguments)
end

def unwrapped_arguments
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-mocks", "~> 3.0"
s.add_dependency "rspec-expectations", "~> 3.0"
s.add_dependency "sidekiq", ">= 5", "< 8"

Expand Down
29 changes: 25 additions & 4 deletions spec/rspec/sidekiq/matchers/be_delayed_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
let(:delay_for_with_arguments_subject) { RSpec::Sidekiq::Matchers::BeDelayed.new(Object).for 3600 }
let(:delay_until_subject) { RSpec::Sidekiq::Matchers::BeDelayed.new.until Time.now + 3600 }
let(:delay_until_with_arguments_subject) { RSpec::Sidekiq::Matchers::BeDelayed.new(Object).until Time.now + 3600 }

around(:each) do |example|
travel_to Time.new(2023, 8, 1, 0, 0, 0, "-05:00") do
example.run
end
end

before(:each) do
delay_subject.matches? Object.method :nil?
delay_with_arguments_subject.matches? Object.method :is_a?
Expand Down Expand Up @@ -73,7 +80,7 @@
expect(delay_until_with_arguments_subject.description).to eq "be delayed until #{Time.now + 3600} with arguments [Object]"
end
end
end
end

describe '#failure_message' do
context 'when expected is a delay' do
Expand Down Expand Up @@ -120,10 +127,16 @@
Object.delay.nil?

expect(delay_subject.matches? Object.method :nil?).to be true
end

Object.delay.is_a? Object
context "and actual contains arguments" do
it "returns true" do
# Acutal has arugment
Object.delay.is_a? Object

expect(delay_subject.matches? Object.method :is_a?).to be true
# Expected didn't specify any arguemtns to match
expect(delay_subject.matches? Object.method :is_a?).to be true
end
end
end

Expand All @@ -133,6 +146,14 @@

expect(delay_with_arguments_subject.matches? Object.method :is_a?).to be true
end

context "and expected uses builtin matchers" do
let(:delay_with_arguments_subject) { RSpec::Sidekiq::Matchers::BeDelayed.new anything }
it "returns true" do
Object.delay.is_a? Object
expect(delay_with_arguments_subject.matches? Object.method(:is_a?)).to be true
end
end
end

context 'when expected is a delay for' do
Expand Down Expand Up @@ -166,7 +187,7 @@
expect(delay_until_with_arguments_subject.matches? Object.method :is_a?).to be true
end
end
end
end

context 'when condition does not match' do
context 'when expected is a delay' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
expect(Sidekiq::Worker).to have_enqueued_sidekiq_job *worker_args
end

context "when using builtin argument matchers" do
it "matches" do
worker.perform_async({"something" => "Awesome", "extra" => "stuff"})
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))
end
end

context 'perform_in' do
let(:worker_args_in) { worker_args + ['in'] }

Expand Down Expand Up @@ -230,6 +239,13 @@
it 'returns false' do
expect(argument_subject.matches? worker).to be false
end

context "and arguments are out of order" do
it "returns false" do
worker.perform_async(*worker_args.reverse)
expect(argument_subject.matches? worker).to be false
end
end
end

context 'when expected are matchers' do
Expand Down

0 comments on commit 8663f9a

Please sign in to comment.