Skip to content

Commit e1fe213

Browse files
bcharnasebjacobs
authored andcommitted
Add support for asserting job is enqueued with no wait. (rspec#1977)
1 parent 9993c74 commit e1fe213

File tree

5 files changed

+85
-2
lines changed

5 files changed

+85
-2
lines changed

features/job_specs/job_spec.feature

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,25 @@ Feature: job spec
6060
When I run `rspec spec/jobs/upload_backups_spec.rb`
6161
Then the example should pass
6262

63+
Scenario: specify that job was enqueued with no wait
64+
Given a file named "spec/jobs/upload_backups_spec.rb" with:
65+
"""ruby
66+
require "rails_helper"
67+
68+
RSpec.describe UploadBackupsJob, :type => :job do
69+
describe "#perform_later" do
70+
it "uploads a backup" do
71+
ActiveJob::Base.queue_adapter = :test
72+
expect {
73+
UploadBackupsJob.set(queue: "low").perform_later('backup')
74+
}.to have_enqueued_job.with('backup').on_queue("low").at(:no_wait)
75+
end
76+
end
77+
end
78+
"""
79+
When I run `rspec spec/jobs/upload_backups_spec.rb`
80+
Then the example should pass
81+
6382
Scenario: specify that job was enqueued with alias block syntax
6483
Given a file named "spec/jobs/upload_backups_spec.rb" with:
6584
"""ruby

features/matchers/have_been_enqueued_matcher.feature

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ Feature: have_been_enqueued matcher
5555
When I run `rspec spec/jobs/upload_backups_job_spec.rb`
5656
Then the examples should all pass
5757

58+
Scenario: Checking job enqueued with no wait
59+
Given a file named "spec/jobs/upload_backups_job_spec.rb" with:
60+
"""ruby
61+
require "rails_helper"
62+
63+
RSpec.describe UploadBackupsJob do
64+
it "matches with enqueued job" do
65+
ActiveJob::Base.queue_adapter = :test
66+
UploadBackupsJob.perform_later
67+
expect(UploadBackupsJob).to have_been_enqueued.at(:no_wait)
68+
end
69+
end
70+
"""
71+
When I run `rspec spec/jobs/upload_backups_job_spec.rb`
72+
Then the examples should all pass
73+
5874
Scenario: Checking job queue name
5975
Given a file named "spec/jobs/upload_backups_job_spec.rb" with:
6076
"""ruby

features/matchers/have_enqueued_job_matcher.feature

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,23 @@ Feature: have_enqueued_job matcher
5656
When I run `rspec spec/jobs/upload_backups_job_spec.rb`
5757
Then the examples should all pass
5858

59+
Scenario: Checking job enqueued with no wait
60+
Given a file named "spec/jobs/upload_backups_job_spec.rb" with:
61+
"""ruby
62+
require "rails_helper"
63+
64+
RSpec.describe UploadBackupsJob do
65+
it "matches with enqueued job" do
66+
ActiveJob::Base.queue_adapter = :test
67+
expect {
68+
UploadBackupsJob.perform_later
69+
}.to have_enqueued_job.at(:no_wait)
70+
end
71+
end
72+
"""
73+
When I run `rspec spec/jobs/upload_backups_job_spec.rb`
74+
Then the examples should all pass
75+
5976
Scenario: Checking job queue name
6077
Given a file named "spec/jobs/upload_backups_job_spec.rb" with:
6178
"""ruby

lib/rspec/rails/matchers/active_job.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def base_message
118118
"#{message_expectation_modifier} #{@expected_number} jobs,".tap do |msg|
119119
msg << " with #{@args}," if @args.any?
120120
msg << " on queue #{@queue}," if @queue
121-
msg << " at #{@at}," if @at
121+
msg << " at #{@at.inspect}," if @at
122122
msg << " but enqueued #{@matching_jobs_count}"
123123
end
124124
end
@@ -149,12 +149,16 @@ def other_attributes_match?(job)
149149

150150
def serialized_attributes
151151
{}.tap do |attributes|
152-
attributes[:at] = @at.to_f if @at
152+
attributes[:at] = serialized_at if @at
153153
attributes[:queue] = @queue if @queue
154154
attributes[:job] = @job if @job
155155
end
156156
end
157157

158+
def serialized_at
159+
@at == :no_wait ? nil : @at.to_f
160+
end
161+
158162
def set_expected_number(relativity, count)
159163
@expectation_type = relativity
160164
@expected_number = case count
@@ -232,6 +236,10 @@ def matches?(job)
232236
# expect {
233237
# HelloJob.set(wait_until: Date.tomorrow.noon, queue: "low").perform_later(42)
234238
# }.to have_enqueued_job.with(42).on_queue("low").at(Date.tomorrow.noon)
239+
#
240+
# expect {
241+
# HelloJob.set(queue: "low").perform_later(42)
242+
# }.to have_enqueued_job.with(42).on_queue("low").at(:no_wait)
235243
def have_enqueued_job(job = nil)
236244
check_active_job_adapter
237245
ActiveJob::HaveEnqueuedJob.new(job)
@@ -264,6 +272,9 @@ def have_enqueued_job(job = nil)
264272
#
265273
# HelloJob.set(wait_until: Date.tomorrow.noon, queue: "low").perform_later(42)
266274
# expect(HelloJob).to have_been_enqueued.with(42).on_queue("low").at(Date.tomorrow.noon)
275+
#
276+
# HelloJob.set(queue: "low").perform_later(42)
277+
# expect(HelloJob).to have_been_enqueued.with(42).on_queue("low").at(:no_wait)
267278
def have_been_enqueued
268279
check_active_job_adapter
269280
ActiveJob::HaveBeenEnqueued.new

spec/rspec/rails/matchers/active_job_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,26 @@ def self.name; "LoggingJob"; end
196196
}.to have_enqueued_job.at(date)
197197
end
198198

199+
it "has an enqueued job when providing at of :no_wait and there is no wait" do
200+
expect {
201+
hello_job.perform_later
202+
}.to have_enqueued_job.at(:no_wait)
203+
end
204+
205+
it "has an enqueued job when not providing at and there is a wait" do
206+
date = Date.tomorrow.noon
207+
expect {
208+
hello_job.set(:wait_until => date).perform_later
209+
}.to have_enqueued_job
210+
end
211+
212+
it "does not have an enqueued job when providing at of :no_wait and there is a wait" do
213+
date = Date.tomorrow.noon
214+
expect {
215+
hello_job.set(:wait_until => date).perform_later
216+
}.to_not have_enqueued_job.at(:no_wait)
217+
end
218+
199219
it "passes with provided arguments" do
200220
expect {
201221
hello_job.perform_later(42, "David")

0 commit comments

Comments
 (0)