Description
Hello everyone !
I've got to report a strange behavior that I observed today with the use of have_enqueud_job
Ruby version: ruby 2.4.1
Rails version: 4.2.10
Rspec version: 3.7
Observed behaviour
Consider the following code :
class MyJob < ActiveJob::Base
def perform(param)
puts "Hello world ! #{param}"
end
end
class JobLauncher
def launch
MyJob.perform_later("first job")
MyJob.perform_later("second job")
end
end
RSpec.describe JobLauncher do
describe "#launch" do
it "works" do
expect {
JobLauncher.new.launch
}.to have_enqueued_job(MyJob)
end
end
end
This spec is failing because two jobs have been enqueued with the following message :
expected to enqueue exactly 1 jobs, but enqueued 2
There is nothing wrong with that and that's the behavior expected.
But when you change the expectation line with :
expect {
JobLauncher.new.launch
}.to have_enqueued_job(MyJob).with("first job")
then the spec does not fail anymore, even though there is still two jobs that have been enqueued.
Expected behaviour
I find this difference of behavior very strange. I would have expected that have_enqueued_job(Job).with(params)
would behave the same as without the with
, just checking the job params extra.
Maybe it is not a bug but a feature. If so, is there a way to check that only one job have been enqueued with RSpec ?
Thank you