使用 rspec 中的 ActiveJob 执行挂起的作业 [英] Execute pending job with ActiveJob in rspec
本文介绍了使用 rspec 中的 ActiveJob 执行挂起的作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个代码来用 Rspec 测试 ActiveJob 和 ActionMailer我不知道如何真正执行所有排队的作业
I have this code to test ActiveJob and ActionMailer with Rspec I 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 执行挂起的作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文