本文介绍了在rspec中使用ActiveJob执行挂起的作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这段代码可以用Rspec
测试ActiveJob和ActionMailer我不知道如何真正执行所有排队的工作
描述做什么
包括ActiveJob :: TestHelper
之后
clear_enqueued_jobs
end
应该通过电子邮件发送做
期望(enqueued_jobs.size)。到eq(1)
结束
结束
解决方案
这是我解决类似问题的方法:
# rails_helper.rb
RSpec.configure do | config |
config.before:example,perform_enqueued:true do
@old_perform_enqueued_jobs = ActiveJob :: Base.queue_adapter.perform_enqueued_jobs
@old_perform_enqueued_at_jobs = ActiveJob :: Base.queue_adapter :Base.queue_adapter.perform_enqueued_jobs = true
ActiveJob :: Base.queue_adapter.perform_enqueued_at_jobs = true
end
config.after:example,perform_enqueued:true do
ActiveJob :: Base.queue_adapter.perform_enqueued_jobs = @old_perform_enqueued_jobs
ActiveJob :: Base.queue_adapter.perform_enqueued_at_jobs = @old_perform_enqueued_atsjobs
end
end $ b $ c $ p>
然后在我们可以使用的规格中:
它应立即执行,perform_enqueued:确实是
SomeJob.perform_later
结束
I have this code to test ActiveJob and ActionMailer with RspecI don't know how really execute all enqueued job
describe 'whatever' do
include ActiveJob::TestHelper
after do
clear_enqueued_jobs
end
it 'should email' do
expect(enqueued_jobs.size).to eq(1)
end
end
解决方案 Here is how I solved a similar problem:
# rails_helper.rb
RSpec.configure do |config|
config.before :example, perform_enqueued: true do
@old_perform_enqueued_jobs = ActiveJob::Base.queue_adapter.perform_enqueued_jobs
@old_perform_enqueued_at_jobs = ActiveJob::Base.queue_adapter.perform_enqueued_at_jobs
ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true
ActiveJob::Base.queue_adapter.perform_enqueued_at_jobs = true
end
config.after :example, perform_enqueued: true do
ActiveJob::Base.queue_adapter.perform_enqueued_jobs = @old_perform_enqueued_jobs
ActiveJob::Base.queue_adapter.perform_enqueued_at_jobs = @old_perform_enqueued_at_jobs
end
end
Then in specs we can use:
it "should perform immediately", perform_enqueued: true do
SomeJob.perform_later
end
这篇关于在rspec中使用ActiveJob执行挂起的作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!