rspec-sidekiq
rspec-sidekiq copied to clipboard
How can I test using "within_sidekiq_retries_exhausted_block" with an outside method
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"?
I can't even get this to work with the example given in the docs using rspec 3.x.
@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
Was this ever solved?
believe https://github.com/philostler/rspec-sidekiq#within_sidekiq_retries_exhausted_block should work
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...
+1 to what @packrat386 said here - you'd set the expectation on the NotifyMailer class as opposed to involving the PostListingsWorker
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)