ruby-on-rails – 在rspec中使用ActiveJob执行待处理的作业

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 在rspec中使用ActiveJob执行待处理的作业前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这个代码用Rspec来测试ActiveJob和ActionMailer
我不知道真正执行所有入职的工作
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

解决方法

这是我如何解决类似的问题:
# 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

然后在规格我们可以使用:

it "should perform immediately",perform_enqueued: true do
  SomeJob.perform_later  
end
原文链接:https://www.f2er.com/ruby/271791.html

猜你在找的Ruby相关文章