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


On Wednesday, 13 February 2019 18:14:30 UTC+1, Jon Rowe wrote:
>
> Hi Gleb
>
> What are you unable to test about them? ParameterizedMailers should act as 
> normal Mailers under test (as we don’t test how they’re created, only the 
> result) and you can see a basic “MailerSpec” here 
> https://relishapp.com/rspec/rspec-rails/v/3-8/docs/mailer-specs.
>
> We do also have Job specs, 
> https://relishapp.com/rspec/rspec-rails/v/3-8/docs/job-specs/job-spec, 
> and coming soon (unreleased but you could use by pointing rspec-rails at 
> GitHub) we’ll have specific helpers for asserting mailer jobs are queued 
> (e.g. have_enqueued) 
>
> Is that of any help?
>
> Jon Rowe
> ---------------------------
> [email protected] <javascript:>
> jonrowe.co.uk
>
> On 12 February 2019 at 15:13, [email protected] <javascript:> 
> wrote:
>
> Hello,
>
> Parameterized Mailers 
> <https://api.rubyonrails.org/v5.1/classes/ActionMailer/Parameterized.html> 
> were added in Rails 5.1. I would like to use them in my project, but I have 
> not being able to write tests for them using RSpec.
> Minitest provides the test helper method assert_enqueued_email_with 
> (documented 
> here 
> <https://api.rubyonrails.org/classes/ActionMailer/TestHelper.html#method-i-assert_enqueued_email_with>).
>  
> Is there an equivalent in RSpec?
>
> Thanks,
> Gleb
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/dd40d650-1833-4bf1-a195-23aadae55043%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to