1

这是我的测试代码:

require 'rails_helper'

describe CookCookieWorker do
  let(:user) { FactoryGirl.create(:user) }
  let(:oven) { user.ovens.first }

  it { is_expected.to be_processed_in :cookies }

  it 'enqueues the cook cookies worker to be processed in 5 seconds' do
    oven.cookies.create!({ fillings: 'Vanilla' })
    cookie = oven.cookies.last

    expect(CookCookieWorker).to have_enqueued_sidekiq_job(cookie.id).in(5.seconds)
  end
end

这就是我得到的结果

expected to have an enqueued CookCookieWorker job
         arguments: [1]
         options: {"in"=>5 seconds}
       found
         arguments: []
         options: []

澄清一下,我after_save在模型中有一个回调,Cookie它以这种方式调用工人:

CookCookieWorker.perform_in(5.seconds, id)

一开始我以为我可能无法以这种方式测试工人,可能模型回调没有效果?但是我尝试在测试中手动调用工人:

require 'rails_helper'

describe CookCookieWorker do
  let(:user) { FactoryGirl.create(:user) }
  let(:oven) { user.ovens.first }

  it { is_expected.to be_processed_in :cookies }

  it 'enqueues the cook cookies worker to be processed in 5 seconds' do
    oven.cookies.create!({ fillings: 'Vanilla' })
    cookie = oven.cookies.last

    CookCookieWorker.perform_in(5.seconds, cookie.id) # Calling it manually
    expect(CookCookieWorker).to have_enqueued_sidekiq_job(cookie.id).in(5.seconds)
  end
end

但我得到相同的结果,测试失败。任何想法?

4

0 回答 0