هنا دليلي على المفهوم:
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
كما أفهم ، يتحقق have_enqueued_job
من أداء المهمة مرة واحدة بالضبط ، لذا فإن expect { my_job.perform }.to have_enqueued_job(OtherJob)
هو نفسه expect { my_job.perform }.to have_enqueued_job(OtherJob).exactly(:once)
.
ولكن في الحالة السلبية يصبح expect { my_job.perform }.not_to have_enqueued_job(OtherJob)
هو نفسه expect { my_job.perform }.not_to have_enqueued_job(OtherJob).exactly(:once)
. لذلك من الواضح تمامًا سبب إرجاع expect { my_job.perform }.not_to have_enqueued_job(OtherJob)
صحيحًا عندما يكون هناك وظائف متعددة من نفس النوع ، لكن السلوك محير.
حاولت باستخدام rspec-rails 3.6.1.
لست متأكدًا مما تطلبه هنا ، يبدو أن مقتطف الشفرة الخاص بك expect { my_job.perform }.not_to have_enqueued_job(OtherJob)
يعني أنك لم تقم بإدراج أي وظائف ، ولديك. التوقعات لا تملك أي معرفة ببعضها البعض.
هل تقول أنه يمر؟ لأن هذا خطأ.
أنا أقول أن expect { my_job.perform }.to have_enqueued_job(OtherJob).at_least(:once)
يمر و expect { my_job.perform }.not_to have_enqueued_job(OtherJob)
يمر
واجهت نفس المشكلة. يمكنك اختراقه باستخدام:
expect{ my_job.perform }.to have_enqueued_job(OtherJob).exactly(0).times
التعليق الأكثر فائدة
واجهت نفس المشكلة. يمكنك اختراقه باستخدام: