繁体   English   中英

Sidekiq 假模式不适用于 rails rspec 测试

[英]Sidekiq fake mode is not working on rails rspec testing

我正在尝试在我的 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? 给我真实

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'
  # ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM