如何测试 ActiveJob 是否已入队? [英] How to test that ActiveJob is enqueued?
本文介绍了如何测试 ActiveJob 是否已入队?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果成功保存记录,我有一个创建操作调用 ActiveJob.
I have a create action that calls an ActiveJob if the record is successfully saved.
def create
@object = Object.new(importer_params)
respond_to do |format|
if @object.save
MyJob.perform_later( @object.id )
format.html { redirect_to @object, notice: t('.notice') }
else
format.html { render :new }
end
end
end
我想测试控制器规范中是否正确调用了作业.
I want to test that the Job is correctly called in a controller spec.
describe "POST #create" do
it {
expect {
post :create, { object: valid_attributes }
}.to change(Object, :count).by(1)
}
it {
expect {
post :create, { object: valid_attributes }
}.to have_enqueued_job(MyJob)
}
end
但是我明白了
Failure/Error:
expect {
post :create, { object: valid_attributes }
}.to have_enqueued_job(MyJob)
expected to enqueue exactly 1 jobs, but enqueued 0
第一次测试通过,所以我知道对象保存成功.测试 ActiveJob 是否已入队的正确方法是什么?
The first test is passing, so I know the Object is saved successfully. What is the correct way to test that an ActiveJob is enqueued?
推荐答案
如果您需要检查您的作业是否已多次入队,您现在可以执行 这个:
If you need to check that your job has been enqueued several times, you can now do this:
expect {
3.times { HelloJob.perform_later }
}.to have_enqueued_job(HelloJob).at_least(2).times
这篇关于如何测试 ActiveJob 是否已入队?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文