Search code examples
ruby-on-railsrubyrspecdelayed-jobrails-activejob

Disable ruby on rails job for test


I'm writing controller test with rspec and after action completed, my job supposed to send email to the admin user. But I'd like to disable this job for my tests or mock it somehow. How can I do this?

I'm using delayed_job_active_record + daemons gems.

class AdminNotificationJob < ActiveJob::Base
  queue_as :default

  def perform(method, parameter)
    User.admin.includes(:profile).each do |admin|
      AdminMailer.send(method, admin, parameter).deliver_later
    end
  end
end

Solution

  • #config/environment/test.rb
    config.active_job.queue_adapter = :test
    
    # in spec
    expect { your_action_that_triggers_job_enqueuing }
      .to have_enqueued_job(AdminNotificationJob)
      .with(your_arguments)