Skip to content

Commit

Permalink
Add "immediately" matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismaximin committed Sep 10, 2023
1 parent 89b5094 commit bfb9e02
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/rspec/sidekiq/matchers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def matches?(options)
private

def at_evaluator(value)
return false if job["at"].to_s.empty?
return value.nil? if job["at"].to_s.empty?
value == Time.at(job["at"]).to_i
end

Expand Down Expand Up @@ -185,6 +185,11 @@ def in(interval)
self
end

def immediately
@expected_options["at"] = nil
self
end

def on(queue)
@expected_options["queue"] = queue
self
Expand Down
29 changes: 29 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 @@ -2,6 +2,7 @@

RSpec.describe RSpec::Sidekiq::Matchers::HaveEnqueuedSidekiqJob do
let(:tomorrow) { DateTime.now + 1 }
let(:yesterday) { DateTime.now - 1 }
let(:interval) { 3.minutes }
let(:argument_subject) { described_class.new worker_args }
let(:matcher_subject) { described_class.new [be_a(String), be_a(Integer), true, be_a(Hash)] }
Expand Down Expand Up @@ -223,6 +224,20 @@
expect(matcher_subject.at(tomorrow + 1).matches? worker).to be false
end
end

context 'and past timestamp matches' do
it 'returns true' do
worker.perform_at(yesterday, *worker_args)
expect(matcher_subject.immediately.matches? worker).to be true
end
end

context 'and past timestamp does not match' do
it 'returns true' do
worker.perform_at(tomorrow, *worker_args)
expect(matcher_subject.immediately.matches? worker).to be false
end
end
end

context 'with #perform_in' do
Expand All @@ -239,6 +254,20 @@
expect(matcher_subject.in(interval + 1.minute).matches? worker).to be false
end
end

context 'and past interval matches' do
it 'returns true' do
worker.perform_in(-1, *worker_args)
expect(matcher_subject.immediately.matches? worker).to be true
end
end

context 'and interval does not match' do
it 'returns false' do
worker.perform_in(1, *worker_args)
expect(matcher_subject.immediately.matches? worker).to be false
end
end
end
end
end
Expand Down

0 comments on commit bfb9e02

Please sign in to comment.