Hi Jon,
Before refactoring to use ParameterizedMailers, I had the following code:
AppointmentMailer.reminder(@appointment, @user).deliver_later
with the corresponding test:
expect(AppointmentMailer).to(receive(:reminder).with(appointment, user)).and_return(appointment_reminder_email)
Once I switched to ParameterizedMailers, my code changed to:
AppointmentMailer.with(appointment: @appointment, recipient: @user).reminder.deliver_later
With this change, the Mailer is invoked with the .
with() method, which means the test will not trigger the receive(:reminder). I tried doing something like this:
expect(AppointmentMailer).to(receive(:with).with(appointment: appointment, recipient: user)).and_return(parameterized_appointment_mailer)
However this didn't work as intended because the .
with() method returns a new Parameterized::Mailer. I wish to write a test that checks that the parameters were passed to the AppointmentMailer and that the reminder method was called and returned the correct object ( in this example that would be the appointment_reminder_email).
The minitest assert method is useful because it accepts the parameters as arguments:
def test_parameterized_email
assert_enqueued_email_with AppointmentMailer, :welcome,
args: {appointment: appointment, recipient: user} do
AppointmentMailer.with(appointment: appointment, recipient: user).reminder.deliver_later
end
end
Does this clarify my use-case?
If there is a better way to test my Mailers in general, please let me know!
Best,
Gleb