Bitwise
Bitwise

Reputation: 8451

Test that Sidekiq job called mailer

I'm testing my Sidekiq job and I'm trying to figure out how to ensure that it called a method within the job with the correct arguments. Here is my actual job:

JOB:

def perform
  csv = Campaign.to_csv
  email = current_sso_user.email

  CampaignMailer.send_csv(email, csv).deliver_now
end

I'd like to build a test that ensures the CampaignMailer.send_csv was called with the correct args.

Here is what I have currently:

TEST:

RSpec.describe CampaignsExortJob, type: :model do
  subject(:job) { CampaignsExportJob.new }

  describe '#perform' do
    let(:campaign) { create(:campaign) }

    it 'sends the csv email' do
      expect(job).to receive(:CampaignMailer.send_csv)
    end
  end
end

But this is a syntax error. Can anyone give me some guidance on how to test this properly? Thank You!

Upvotes: 0

Views: 456

Answers (1)

ruby_newbie
ruby_newbie

Reputation: 3283

From the rspec docs(https://relishapp.com/rspec/rspec-mocks/v/2-14/docs/message-expectations/expect-message-using-should-receive):

expect a message with an argument Given a file named "spec/account_spec.rb" with:

require "account"
require "spec_helper"

describe Account do
  context "when closed" do
    it "logs an account closed message" do
      logger = double("logger")
      account = Account.new logger

      logger.should_receive(:account_closed).with(account)

      account.close
    end
  end
end

And a file named "lib/account.rb" with:

Account = Struct.new(:logger) do
  def close
    logger.account_closed(self)
  end
end

When I run rspec spec/account_spec.rb Then the output should contain "1 example, 0 failures"

so in your case:

CampaignMailer.should_receive(:send_csv).with(<expected_email_arg>, <expected_csv_arg>)

You can also add on a .and_return(<expected_return_value>) to test the return value.

Upvotes: 1

Related Questions