Lamps in the mail preview

Having the following email program preview code:

class RegistrationMailerPreview < ActionMailer::Preview # Preview this email at http://localhost:3000/rails/mailers/registration_mailer/welcome def welcome RegistrationMailer.welcome users(:one) end end 

(full file).

That my settings ( users(:one) ) cannot be reached, return the error status 500 and print the following error:

 NoMethodError: undefined method `users' for #RegistrationMailerPreview 

Can we get instrument recordings from the mail view?

If so, I would like to know how to do it.
I saw that it should be possible here , but I cannot require test_helper in this file (I do not know why), and I do not understand the difference between ActionMailer::TestCase and ActionMailer::Preview .

If not, is there a way to view mail without sending as a User.first parameter, since I could run my tests on a machine that has no data populated in the database.

+5
source share
1 answer

I don’t know about the standard binding infrastructure, but I can tell, using FactoryBot for my devices, that I was able to use them in my email previews by simply adding build / create methods with FactoryBot . I did not need require anything at the top of the file. i.e:.

 class RegistrationMailerPreview < ActionMailer::Preview def welcome user = FactoryBot.create(:user) RegistrationMailer.welcome(user) end end 

To answer the second question, you can also simply replace the instrument line above with user = User.new(firstname: "Joe") . This will create a new user that will be used in the preview without storing it in the database.

0
source

All Articles