首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rspec-sidekiq:如何用另一个类方法测试"within_sidekiq_retries_exhausted_block“

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

Stack Overflow用户
提问于 2015-11-26 11:41:19
回答 1查看 2.4K关注 0票数 3

我使用gem "rspec-sidekiq“来测试"sidekiq_retries_exhausted”。

这是我的工人:

代码语言:javascript
复制
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的例子:

代码语言:javascript
复制
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“块中的方法作为符号输入到测试中。我沿着这条路走了,但它不起作用。

下面是我在test.Look ()方法中的接收:

代码语言:javascript
复制
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

这是我的日志:

代码语言:javascript
复制
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“期间测试属于另一个类的方法呢?也许可以找到一些方法将方法作为symblo发送?

EN

回答 1

Stack Overflow用户

发布于 2016-05-12 20:06:17

你已经很接近了。但是,您可以简单地针对NotifyMailer编写您的期望,如下所示:

代码语言:javascript
复制
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)
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33930199

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档