Execute pending job with ActiveJob in rspec

后端 未结 3 1362
伪装坚强ぢ
伪装坚强ぢ 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:24

    Just combined all the best pieces, +included sidekiq:

    spec/support/perform_jobs.rb:

    require 'sidekiq/testing'
    
    RSpec.configure do |config|
      Sidekiq::Testing.fake!
    
      config.around(:each, perform_jobs: true) do |example|
        Sidekiq::Testing.inline! do
          queue_adapter = ActiveJob::Base.queue_adapter
          old_perform_enqueued_jobs = queue_adapter.perform_enqueued_jobs
          old_perform_enqueued_at_jobs = queue_adapter.perform_enqueued_at_jobs
          queue_adapter.perform_enqueued_jobs = true
          queue_adapter.perform_enqueued_at_jobs = true
          example.run
        ensure
          queue_adapter.perform_enqueued_jobs = old_perform_enqueued_jobs
          queue_adapter.perform_enqueued_at_jobs = old_perform_enqueued_at_jobs
        end
      end
    
    end
    
    

    spec/some_spec.rb:

    it 'works', perform_jobs: true do
      ...
    end
    

提交回复
热议问题