禁用Ruby on Rails中的测试作业

8
我正在使用rspec编写控制器测试,在操作完成后,我的任务应该向管理员用户发送电子邮件。但是我想在测试中禁用此任务或以某种方式模拟它。我该如何做?
我正在使用delayed_job_active_recorddaemons宝石。
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
2个回答

10
#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)

1
config.active_job.queue_adapter = :test - 这就是我正在寻找的,谢谢! - divideByZero

1
您的控制器测试应该如下所示:

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

这里使用了在rspec-rails中发现的#have_enqueued_job方法。


1
同时 :) +1 - Andrey Deineko
1
我认为这被称为“双胞胎”? ;) - Anthony

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接