Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
zhen6939 opened this issue Nov 24, 2015 · 7 comments
Labels

Comments

@zhen6939
Copy link

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"?

@gditrick
Copy link

gditrick commented Dec 4, 2015

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

@chhhris
Copy link

chhhris commented Oct 12, 2016

@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

@chrishough
Copy link

Was this ever solved?

@packrat386
Copy link
Collaborator

@packrat386
Copy link
Collaborator

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...

@paulmthiebauth
Copy link

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

@drewnichols
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants