【问题标题】:rspec-sidekiq: How to test "within_sidekiq_retries_exhausted_block" with another class methodrspec-sidekiq:如何用另一个类方法测试“within_sidekiq_retries_exhausted_block”
【发布时间】:2016-02-29 01:13:43
【问题描述】:

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

这是我的工人:

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-sidekiq” 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_exhausted”块中的方法作为符号输入到测试中。 我按照这个方法,但它不起作用。

这是我的测试。看看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_exhausted”期间测试一个方法是否属于另一个类? 也许找到一些方法将方法作为符号发送?

【问题讨论】:

    标签: ruby-on-rails rspec sidekiq rspec-sidekiq


    【解决方案1】:

    你已经接近了。但是,您可以像这样简单地写出您对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)
    }
    

    【讨论】:

    • 这是唯一的测试方法吗?
    猜你喜欢
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    • 2017-02-08
    相关资源
    最近更新 更多