Question: Disable ruby on rails job for test

Question

Disable ruby on rails job for test

Answers 2
Added at 2016-11-30 16:11
Tags
Question

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
Answers
nr: #1 dodano: 2016-11-30 16:11
#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)
nr: #2 dodano: 2016-11-30 16:11

Your controller test would look like this:

it "enqueues a AdminNotification" do
  expect {
    get :your_controller_action, {}
  }.to have_enqueued_job(AdminNotificationJob)
end

This uses the #have_enqueued_job method found on rspec-rails

Source Show
◀ Wstecz