Eason Caizhen Liu
Eason Caizhen Liu

Reputation: 459

rspec-sidekiq: How to test "within_sidekiq_retries_exhausted_block" with another class method

I am using gem "rspec-sidekiq" to test "sidekiq_retries_exhausted".

This is my worker:

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

This is an example from "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')
}

I think it should input methods in "sidekiq_retries_exhausted" block as symbol into test. I followed this way, but it doesn't work.

Here is my test.Look at the receive() method.:

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

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"? Maybe find some way to send method as symblo?

Upvotes: 3

Views: 4747

Answers (1)

codener
codener

Reputation: 486

You're close. However, you can simply write your expectation against the NotifyMailer like this:

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)
}

Upvotes: 3

Related Questions