diff --git a/lib/rspec/rails/matchers/active_job.rb b/lib/rspec/rails/matchers/active_job.rb index be8e732b4b..0555c9fda3 100644 --- a/lib/rspec/rails/matchers/active_job.rb +++ b/lib/rspec/rails/matchers/active_job.rb @@ -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 @@ -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 diff --git a/spec/rspec/rails/matchers/active_job_spec.rb b/spec/rspec/rails/matchers/active_job_spec.rb index 18e0222171..c85b98a6b2 100644 --- a/spec/rspec/rails/matchers/active_job_spec.rb +++ b/spec/rspec/rails/matchers/active_job_spec.rb @@ -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 @@ -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 diff --git a/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb b/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb index 581cc5f088..5ff4291087 100644 --- a/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb +++ b/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb @@ -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