【问题标题】:Disable ruby on rails job for test禁用 ruby​​ on rails 作业进行测试
【发布时间】:2017-04-14 23:24:42
【问题描述】:

我正在使用 rspec 编写控制器测试,操作完成后,我的工作应该是向管理员用户发送电子邮件。但我想为我的测试禁用这项工作或以某种方式模拟它。我该怎么做?

我正在使用 delayed_job_active_record + daemons 宝石。

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

【问题讨论】:

    标签: ruby-on-rails ruby rspec delayed-job rails-activejob


    【解决方案1】:
    #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)
    

    【讨论】:

    • config.active_job.queue_adapter = :test - 这就是我要找的,谢谢!
    【解决方案2】:

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

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

    这使用rspec-rails 上的#have_enqueued_job 方法

    【讨论】:

    • 同时 :) +1
    • 我认为这叫做“孪生”? ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 2016-01-06
    • 2013-03-05
    • 2013-06-14
    • 1970-01-01
    相关资源
    最近更新 更多