A
A
A_Modestov2016-06-10 10:56:44
Ruby on Rails
A_Modestov, 2016-06-10 10:56:44

How to test mailer call?

Welcome all. The following question arose, it is necessary to test the method call at the controller level:
ConfirmOauth.email_confirmation(user).deliver_now
How to implement this in the case of a mailer, where 2 methods are called at once? The following options, of course, do not work:

- expect(ConfirmOauth.email_confirmation(user)).to receive(:deliver_now)
- expect(ConfirmOauth).to receive(:email_confirmation).with(user)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Sidorov, 2016-06-10
@A_Modestov

let(:confirm_oauth) { double deliver_now: nil }
before do
  allow(ConfirmOauth).to receive(:email_confirmation)
    .and_return confirm_oauth
end
subject! { do_something_that_calls_mailer }

it do
  expect(ConfirmOauth).to have_received(:email_confirmation).with user
  expect(confirm_oauth).to have_received :deliver_now
end

Another way is the receive_message_chain method https://relishapp.com/rspec/rspec-mocks/docs/worki... It is suitable if you do not need to check with what parameters intermediate methods were called.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question