【问题标题】:Sidekiq fake mode is not working on rails rspec testingSidekiq 伪造模式不适用于 rails rspec 测试
【发布时间】:2021-07-23 12:37:34
【问题描述】:

我正在尝试在我的 Rails 代码上测试工作我有以下内容

RSpec.describe MyJob, type: :job do

  it 'job in correct queue' do
    VCR.use_cassette('mycassette/type') do
      described_class.perform_later(id)
      assert_equal 1, described_class.jobs.size
    end
  end
end

上面给我一个错误undefined method jobs我已经测试过,似乎我在正确的模式下运行, Sidekiq::Testing.fake?给我真实

【问题讨论】:

    标签: ruby-on-rails rspec sidekiq


    【解决方案1】:

    perform_later 表示您使用的是 ActiveJob 而不是直接使用 Sidekiq,因此您还必须使用 ActiveJob 的测试助手:https://edgeapi.rubyonrails.org/classes/ActiveJob/TestHelper.html

    assert_enqueued_jobs 1 do
      described_class.perform_later(id)
    end
    

    或者,您可以通过从以下位置更改作业定义来完全删除 ActiveJob:

    class MyJob < ActiveJob::Base
      queue_as :my_queue
      # ...
    

    到:

    class MyJob
      include Sidekiq::Worker
      sidekiq_options queue: 'my_queue'
      # ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-31
      相关资源
      最近更新 更多