How to check what is queued in ActiveJob using Rspec

后端 未结 7 1231
感动是毒
感动是毒 2021-01-31 08:33

I\'m working on a reset_password method in a Rails API app. When this endpoint is hit, an ActiveJob is queued that will fire off a request to Mandrill (our transactional email c

7条回答
  •  逝去的感伤
    2021-01-31 08:45

    Testing Rails ActiveJob with RSpec

    class MyJob < ActiveJob::Base
      queue_as :urgent
    
      rescue_from(NoResultsError) do
        retry_job wait: 5.minutes, queue: :default
      end
    
      def perform(*args)
        MyService.call(*args)
      end
    end
    
    require 'rails_helper'
    
    RSpec.describe MyJob, type: :job do
      include ActiveJob::TestHelper
    
      subject(:job) { described_class.perform_later(123) }
    
      it 'queues the job' do
        expect { job }
          .to change(ActiveJob::Base.queue_adapter.enqueued_jobs, :size).by(1)
      end
    
      it 'is in urgent queue' do
        expect(MyJob.new.queue_name).to eq('urgent')
      end
    
      it 'executes perform' do
        expect(MyService).to receive(:call).with(123)
        perform_enqueued_jobs { job }
      end
    
      it 'handles no results error' do
        allow(MyService).to receive(:call).and_raise(NoResultsError)
    
        perform_enqueued_jobs do
          expect_any_instance_of(MyJob)
            .to receive(:retry_job).with(wait: 10.minutes, queue: :default)
    
          job
        end
      end
    
      after do
        clear_enqueued_jobs
        clear_performed_jobs
      end
    end
    

提交回复
热议问题