Skip to content

Commit

Permalink
enqueue_job alias for have_enqueued_job
Browse files Browse the repository at this point in the history
  • Loading branch information
DarthSim committed May 4, 2016
1 parent f4d58a1 commit 7f67b8c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
19 changes: 18 additions & 1 deletion 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
Expand Down Expand Up @@ -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
8 changes: 7 additions & 1 deletion lib/rspec/rails/matchers/active_job.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions spec/rspec/rails/matchers/active_job_spec.rb
Expand Up @@ -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 {
Expand Down

0 comments on commit 7f67b8c

Please sign in to comment.