diff --git a/features/matchers/have_enqueued_job_matcher.feature b/features/matchers/have_enqueued_job_matcher.feature index 924fb329ef..f1b99bbf2a 100644 --- a/features/matchers/have_enqueued_job_matcher.feature +++ b/features/matchers/have_enqueued_job_matcher.feature @@ -1,6 +1,6 @@ Feature: have_enqueued_job matcher - The `have_enqueued_job` matcher is used to check if given ActiveJob job was enqueued. + The `have_enqueued_job` (also known as `enqueue_job`) matcher is used to check if given ActiveJob job was enqueued. Background: Given active job is available @@ -72,3 +72,20 @@ Feature: have_enqueued_job matcher """ When I run `rspec spec/jobs/upload_backups_job_spec.rb` Then the examples should all pass + + Scenario: Using alias method + 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 enqueue_job(UploadBackupsJob) + end + end + """ + When I run `rspec spec/jobs/upload_backups_job_spec.rb` + Then the examples should all pass diff --git a/lib/rspec/rails/matchers/active_job.rb b/lib/rspec/rails/matchers/active_job.rb index 1b9e007bc3..be35098736 100644 --- a/lib/rspec/rails/matchers/active_job.rb +++ b/lib/rspec/rails/matchers/active_job.rb @@ -147,7 +147,7 @@ def initialize(job) end def matches?(proc) - raise ArgumentError, "have_enqueued_job only support block expectations" unless Proc === proc + raise ArgumentError, "have_enqueued_job and enqueue_job only support block expectations" unless Proc === proc original_enqueued_jobs_count = queue_adapter.enqueued_jobs.count proc.call @@ -174,6 +174,11 @@ def matches?(job) # HeavyLiftingJob.perform_later # }.to have_enqueued_job # + # # Using alias + # expect { + # HeavyLiftingJob.perform_later + # }.to enqueue_job + # # expect { # HelloJob.perform_later # HeavyLiftingJob.perform_later @@ -201,6 +206,7 @@ def have_enqueued_job(job = nil) check_active_job_adapter ActiveJob::HaveEnqueuedJob.new(job) end + alias_method :enqueue_job, :have_enqueued_job # @api public # Passess if `count` of jobs were enqueued diff --git a/spec/rspec/rails/matchers/active_job_spec.rb b/spec/rspec/rails/matchers/active_job_spec.rb index f537387436..dc1b200cb3 100644 --- a/spec/rspec/rails/matchers/active_job_spec.rb +++ b/spec/rspec/rails/matchers/active_job_spec.rb @@ -67,6 +67,12 @@ def to_global_id(options = {}) }.to have_enqueued_job end + it "passess when using alias" do + expect { + heavy_lifting_job.perform_later + }.to enqueue_job + end + it "counts only jobs enqueued in block" do heavy_lifting_job.perform_later expect {