Execute pending job with ActiveJob in rspec

后端 未结 3 1361
伪装坚强ぢ
伪装坚强ぢ 2021-02-12 19:49

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::         


        
3条回答
  •  余生分开走
    2021-02-12 20:25

    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
    

提交回复
热议问题