Skip to content

Negative have_enqueued_job does not work as expected with multiple jobs of same kind #1870

Closed
@povilasjurcys

Description

@povilasjurcys

Here is my proof of concept:

require 'spec_helper'

class MyJob < ActiveJob::Base
  def perform
    OtherJob.perform_later
    OtherJob.perform_later
  end
end

class OtherJob < ActiveJob::Base
end

describe MyJob do
  subject(:my_job) { MyJob.new }

  it 'enqueues job' do
    expect { my_job.perform }.to have_enqueued_job(OtherJob).at_least(:once)
    expect { my_job.perform }.not_to have_enqueued_job(OtherJob)
  end
end

As I understand, have_enqueued_job checks if job was performed exactly once, so expect { my_job.perform }.to have_enqueued_job(OtherJob) is same as expect { my_job.perform }.to have_enqueued_job(OtherJob).exactly(:once).

But in negative case expect { my_job.perform }.not_to have_enqueued_job(OtherJob) becomes same as expect { my_job.perform }.not_to have_enqueued_job(OtherJob).exactly(:once). So it's quite clear why expect { my_job.perform }.not_to have_enqueued_job(OtherJob) returns true when there is multiple jobs of same kind, but behavior is confusing.

Tried with rspec-rails 3.6.1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions