rspec-sidekiq icon indicating copy to clipboard operation
rspec-sidekiq copied to clipboard

How can I test using "within_sidekiq_retries_exhausted_block" with an outside method

Open zhen6939 opened this issue 9 years ago • 7 comments

I wanna test "sidekiq_retries_exhausted" in this class:

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

Here is my test:

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

But it doesn't work.Here is my log:

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

So,is there any way to test a method belong to another class during "sidekiq_retries_exhausted"?

zhen6939 avatar Nov 24 '15 09:11 zhen6939

I can't even get this to work with the example given in the docs using rspec 3.x.

gditrick avatar Dec 04 '15 05:12 gditrick

@zhen639 did you ever solve this?

Update - found this answer on SO from Mike Perham, which seems relevant: http://stackoverflow.com/questions/32874510/how-can-i-test-sidekiq-job-failures

chhhris avatar Oct 12 '16 18:10 chhhris

Was this ever solved?

chrishough avatar Apr 29 '20 01:04 chrishough

believe https://github.com/philostler/rspec-sidekiq#within_sidekiq_retries_exhausted_block should work

packrat386 avatar Jun 21 '20 16:06 packrat386

Err actually I misread this. I think you need to just set the expectation on the class you're calling. In the example case it'd be expect(NotifyMailer).to receive(:warn_email) # etc...

packrat386 avatar Jun 21 '20 16:06 packrat386

+1 to what @packrat386 said here - you'd set the expectation on the NotifyMailer class as opposed to involving the PostListingsWorker

paulmthiebauth avatar Jan 13 '21 16:01 paulmthiebauth

I recommend closing this issue. I was able to get within_sidekiq_retries_exhausted_block working successfully as described in the documentation. The issue described above is because

expect(PostListingsWorker).to receive(:"NotifyMailer.warn_email().deliver_now").with(msg)

should be

expect(NotifyMailer).to receive(:warn_email).with(msg)

drewnichols avatar May 25 '22 15:05 drewnichols