rspec-sidekiq:如何使用另一个类方法测试"within_sidekiq_retries_exhausted_block"



我正在使用gem"rspec sidekiq"来测试"sidekiq_retries_executted"。

这是我的员工:

class PostListingsWorker
  include Sidekiq::Worker
  sidekiq_options :retry => 0
  sidekiq_retries_exhausted do |msg|
    NotifyMailer.warn_email(msg).deliver_now
  end
  def perform(params, fetch_time)
  ....
  end
end

这是一个来自"rspec-sidkiq"github:的例子

sidekiq_retries_exhausted do |msg|
  bar('hello')
end
# test with...
FooClass.within_sidekiq_retries_exhausted_block {
  expect(FooClass).to receive(:bar).with('hello')
}

我认为它应该将"sidekiq_retries_executted"块中的方法作为符号输入到测试中。我按这个方法走,但没用。

这是我的测试。看看receive()方法。:

it 'should send email when retries exhausted' do
    msg = {error_message: 'Wrong', args: [{id: 1}]}.to_json
    PostListingsWorker.within_sidekiq_retries_exhausted_block(msg) {
      expect(PostListingsWorker).to receive(:"NotifyMailer.warn_email().deliver_now").with(msg)
    }
 end

这是我的日志:

Failure/Error: expect(PostListingsWorker).to > >
receive(:"NotifyMailer.warn_email().deliver_now").with(:msg)
PostListingsWorker does not implement: NotifyMailer.warn_email().deliver_now

那么,有什么方法可以在"sidekiq_retries_executted"期间测试属于另一个类的方法吗?也许可以找到某种方法将方法作为symblo发送?

你很接近。然而,您可以简单地针对NotifyMailer写下您的期望,如下所示:

email = double('email')
msg = double('msg')
PostListingsWorker.within_sidekiq_retries_exhausted_block(msg) {
  expect(NotifyMailer).to receive(:warn_email).with(msg).and_return(email)
  expect(email).to receive(:deliver_now)
}

最新更新