Jwan622
Jwan622

Reputation: 11649

Mock multiple methods in Rspec where the first method takes arguments

I have a class that makes a command call to another object and I'm trying to test this in RSpec.

SendWithUsInviteeMailer.send_invite(invitee_id).deliver

I'd like to write something like this:

expect(SendWithUsInviteeMailer).to receive_message_chain(:send_invite, :deliver).with(invitee.id)

where the invitee.id is sent as an argument to the first method. How do I test this correctly? Is there a way to using message chain?

Upvotes: 1

Views: 2197

Answers (1)

Jan Klimo
Jan Klimo

Reputation: 4930

I'd probably stub the send_invite method and check for deliveries, like so:

# config/environments/test.rb
config.action_mailer.delivery_method = :test

And in your spec:

before do
  ActionMailer::Base.deliveries.clear
  allow(SendWithUsInviteeMailer).to receive(:send_invite).and_call_original
end

it 'sends out the right email'
  expect(SendWithUsInviteeMailer).to receive(:send_invite).with(invitee.id)
  # perform the action to be tested
  expect(ActionMailer::Base.deliveries.count).to eq 1
end

Upvotes: 1

Related Questions