Skip to content

Commit

Permalink
Backport: Change negated active job matcher (#2069)
Browse files Browse the repository at this point in the history
This is a partial backport of #2069 that doesn't include changes on
"have_enqueued_mail" tests.

When expecting no job to be enqueued, the matcher would expect exactly
one job not to have been enqueued. This commit changes so that it now expects
no jobs to have been enqueued.
  • Loading branch information
Istanful authored and benoittgt committed Jan 17, 2019
1 parent 7ca3d04 commit 9271c22
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/rspec/rails/matchers/active_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ def matches?(proc)

check(in_block_jobs)
end

def does_not_match?(proc)
set_expected_number(:at_least, 1)

!matches?(proc)
end
end

# @private
Expand All @@ -205,6 +211,12 @@ def matches?(job)
@job = job
check(queue_adapter.enqueued_jobs)
end

def does_not_match?(proc)
set_expected_number(:at_least, 1)

!matches?(proc)
end
end
end

Expand Down
19 changes: 18 additions & 1 deletion spec/rspec/rails/matchers/active_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,16 @@ def self.name; "LoggingJob"; end
it "fails when negated and job is enqueued" do
expect {
expect { heavy_lifting_job.perform_later }.not_to have_enqueued_job
}.to raise_error(/expected not to enqueue exactly 1 jobs, but enqueued 1/)
}.to raise_error(/expected not to enqueue at least 1 jobs, but enqueued 1/)
end

it "fails when negated and several jobs enqueued" do
expect {
expect {
heavy_lifting_job.perform_later
heavy_lifting_job.perform_later
}.not_to have_enqueued_job
}.to raise_error(/expected not to enqueue at least 1 jobs, but enqueued 2/)
end

it "passes with job name" do
Expand Down Expand Up @@ -344,5 +353,13 @@ def self.name; "LoggingJob"; end
expect(heavy_lifting_job).to have_been_enqueued
}.to raise_error(/expected to enqueue exactly 1 jobs, but enqueued 0/)
end

it "fails when negated and several jobs enqueued" do
heavy_lifting_job.perform_later
heavy_lifting_job.perform_later
expect {
expect(heavy_lifting_job).not_to have_been_enqueued
}.to raise_error(/expected not to enqueue at least 1 jobs, but enqueued 2/)
end
end
end

0 comments on commit 9271c22

Please sign in to comment.