Skip to content

Change negated active job matcher, fixes #1870 #2069

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
Jan 15, 2019
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
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
2 changes: 1 addition & 1 deletion spec/rspec/rails/matchers/have_enqueued_mail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def email_with_optional_args(required_arg, optional_arg = nil); end
expect {
TestMailer.test_email.deliver_later
}.not_to have_enqueued_mail(TestMailer, :test_email)
}.to raise_error(/expected not to enqueue TestMailer.test_email exactly 1 time but enqueued 1/)
}.to raise_error(/expected not to enqueue TestMailer.test_email at least 1 time but enqueued 1/)
end

it "passes with :once count" do
Expand Down