Skip to content

Add support for asserting job is enqueued with no wait. #1977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions features/job_specs/job_spec.feature
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ Feature: job spec
When I run `rspec spec/jobs/upload_backups_spec.rb`
Then the example should pass

Scenario: specify that job was enqueued with no wait
Given a file named "spec/jobs/upload_backups_spec.rb" with:
"""ruby
require "rails_helper"

RSpec.describe UploadBackupsJob, :type => :job do
describe "#perform_later" do
it "uploads a backup" do
ActiveJob::Base.queue_adapter = :test
expect {
UploadBackupsJob.set(queue: "low").perform_later('backup')
}.to have_enqueued_job.with('backup').on_queue("low").at(:no_wait)
end
end
end
"""
When I run `rspec spec/jobs/upload_backups_spec.rb`
Then the example should pass

Scenario: specify that job was enqueued with alias block syntax
Given a file named "spec/jobs/upload_backups_spec.rb" with:
"""ruby
Expand Down
16 changes: 16 additions & 0 deletions features/matchers/have_been_enqueued_matcher.feature
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ Feature: have_been_enqueued matcher
When I run `rspec spec/jobs/upload_backups_job_spec.rb`
Then the examples should all pass

Scenario: Checking job enqueued with no wait
Given a file named "spec/jobs/upload_backups_job_spec.rb" with:
"""ruby
require "rails_helper"

RSpec.describe UploadBackupsJob do
it "matches with enqueued job" do
ActiveJob::Base.queue_adapter = :test
UploadBackupsJob.perform_later
expect(UploadBackupsJob).to have_been_enqueued.at(:no_wait)
end
end
"""
When I run `rspec spec/jobs/upload_backups_job_spec.rb`
Then the examples should all pass

Scenario: Checking job queue name
Given a file named "spec/jobs/upload_backups_job_spec.rb" with:
"""ruby
Expand Down
17 changes: 17 additions & 0 deletions features/matchers/have_enqueued_job_matcher.feature
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ Feature: have_enqueued_job matcher
When I run `rspec spec/jobs/upload_backups_job_spec.rb`
Then the examples should all pass

Scenario: Checking job enqueued with no wait
Given a file named "spec/jobs/upload_backups_job_spec.rb" with:
"""ruby
require "rails_helper"

RSpec.describe UploadBackupsJob do
it "matches with enqueued job" do
ActiveJob::Base.queue_adapter = :test
expect {
UploadBackupsJob.perform_later
}.to have_enqueued_job.at(:no_wait)
end
end
"""
When I run `rspec spec/jobs/upload_backups_job_spec.rb`
Then the examples should all pass

Scenario: Checking job queue name
Given a file named "spec/jobs/upload_backups_job_spec.rb" with:
"""ruby
Expand Down
15 changes: 13 additions & 2 deletions lib/rspec/rails/matchers/active_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def base_message
"#{message_expectation_modifier} #{@expected_number} jobs,".tap do |msg|
msg << " with #{@args}," if @args.any?
msg << " on queue #{@queue}," if @queue
msg << " at #{@at}," if @at
msg << " at #{@at.inspect}," if @at
msg << " but enqueued #{@matching_jobs_count}"
end
end
Expand Down Expand Up @@ -149,12 +149,16 @@ def other_attributes_match?(job)

def serialized_attributes
{}.tap do |attributes|
attributes[:at] = @at.to_f if @at
attributes[:at] = serialized_at if @at
attributes[:queue] = @queue if @queue
attributes[:job] = @job if @job
end
end

def serialized_at
@at == :no_wait ? nil : @at.to_f
end

def set_expected_number(relativity, count)
@expectation_type = relativity
@expected_number = case count
Expand Down Expand Up @@ -232,6 +236,10 @@ def matches?(job)
# expect {
# HelloJob.set(wait_until: Date.tomorrow.noon, queue: "low").perform_later(42)
# }.to have_enqueued_job.with(42).on_queue("low").at(Date.tomorrow.noon)
#
# expect {
# HelloJob.set(queue: "low").perform_later(42)
# }.to have_enqueued_job.with(42).on_queue("low").at(:no_wait)
def have_enqueued_job(job = nil)
check_active_job_adapter
ActiveJob::HaveEnqueuedJob.new(job)
Expand Down Expand Up @@ -264,6 +272,9 @@ def have_enqueued_job(job = nil)
#
# HelloJob.set(wait_until: Date.tomorrow.noon, queue: "low").perform_later(42)
# expect(HelloJob).to have_been_enqueued.with(42).on_queue("low").at(Date.tomorrow.noon)
#
# HelloJob.set(queue: "low").perform_later(42)
# expect(HelloJob).to have_been_enqueued.with(42).on_queue("low").at(:no_wait)
def have_been_enqueued
check_active_job_adapter
ActiveJob::HaveBeenEnqueued.new
Expand Down
20 changes: 20 additions & 0 deletions spec/rspec/rails/matchers/active_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,26 @@ def self.name; "LoggingJob"; end
}.to have_enqueued_job.at(date)
end

it "has an enqueued job when providing at of :no_wait and there is no wait" do
expect {
hello_job.perform_later
}.to have_enqueued_job.at(:no_wait)
end

it "has an enqueued job when not providing at and there is a wait" do
date = Date.tomorrow.noon
expect {
hello_job.set(:wait_until => date).perform_later
}.to have_enqueued_job
end

it "does not have an enqueued job when providing at of :no_wait and there is a wait" do
date = Date.tomorrow.noon
expect {
hello_job.set(:wait_until => date).perform_later
}.to_not have_enqueued_job.at(:no_wait)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's another scenario here, not setting at at all and there being a wait

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good callouts 👍


it "passes with provided arguments" do
expect {
hello_job.perform_later(42, "David")
Expand Down