Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Rails test "National Day" to add mail sending program and test mail sending program

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

When it comes to testing the mail sending program, we first need to allow the system to send mail. Let's first add the ability to send mail to the system.

Action Mailer in Rails 3 is a good video tutorial, which you can refer to.

There is also a more detailed introduction in http://guides.rubyonrails.org/action_mailer_basics.html.

Add mail sender

To send email, make sure that we have a target mailbox, so our users should have a property: email.

Or take my blog project as an exercise project.

We send an email to the user when he registers.

First let's create an initialization file

Config/initializers/setup_mail.rb, initialize mailbox-related information.

ActionMailer::Base.smtp_settings = {: address = > "smtp.163.com",: port = > 25 163.com = > user_name = > "woaiguanshui2012",: password = > "123456asdf",: authentication = > "plain",: enable_starttls_auto = > true}

We use 163mailbox as the mail sending server, we need to have a registered user on it, here I register a woaiguanshui201, the password is 123456asdf.

Create a mail sender

Then create a mailer program with the rails g mailer command.

Rails g mailer user_mailer

Command creates an app/mailers/user_mailer.rb file.

Class UserMailer

< ActionMailer::Basedefault :from =>

"woaiguanshui2012@163.com" end

Add a method to send emails to users when they sign up.

Class UserMailer

< ActionMailer::Basedefault :from =>

"woaiguanshui2012@163.com" def registration_confirmation (user) mail (: to = > user.email,: subject = > "Registered") end end

Create a message content template

The content of the message can also be achieved through a template, just as the action counterpart of controller should have a view. Our mailer method also requires a view as a template.

Create an app/views/user_mailer/registration_confirmation.text.erb file and write the following in the file.

Thank you for registering!

Call the mail sender

The last step up and down is that after the user has successfully registered, call the registration_confirmation method of the mailer program, and you can send the contents of our predefined template file.

Let's modify the user registration method, userscontroller's create method.

Def create@user = User.new (params [: user]) if @ user.saveUserMailer.registration_confirmation (@ user) .deliverflash [: notice] = "sign up successfully!" signin (@ user) redirect_to root_pathelseflash.now [: notice] = "sign up failed!" render: newendend

UserMailer.registration_confirmation (@ user) .deliver

The above line is new to us, which is used to send e-mail.

At this point, start our service rails s, then register a user, and then check the user's mailbox, and you should have an email from woaiguanshui2012@163 with the title Registered and the content thank you for registering!.

This means that our email sender is working properly.

Rich message content template

We can also enrich the content of the email, such as adding user information. Modify the registration_confirmation.text.erb file.

Hi, Thank you for registering!Welcome to come back!

This requires us to add a returned variable @ user to the registration_comfirmation method.

Def registration_confirmation (user) @ user = usermail (: to = > user.email,: subject = > "Registered") end

If you use url in your template, such as edit_user_url (@ user.id,: host = > "localhost:3000"), you can only use the _ url method, not the _ path method, and you need to specify host. This is because the mail client does not have the context of web, unlike the context of web in controller, so there is no need to specify host. Of course, it is troublesome to specify each time, and you can also add the following to the configuration file config/initializers/setup_mail.rb created earlier.

ActionMailer::Base.default_url_options [: host] = "localhost:3000"

This allows you to omit the host parameter in url.

Add attachments to the message

Def registration_confirmation (user) @ user = userp_w_uploads ["rails.png"] = File.read ("# {Rails.root} / public/p_w_picpaths/rails.png") mail (: to = > "# {user.name}",: subject = > "Registered") end

Register the user again, and then log in to the user's mailbox, and you will find that you have received an email with an attachment.

There is still a lot about sending mail, and you will have more experience in the process of use and design. For example, our current email and user registration are processed synchronously, so that a large number of user registrations will seriously slow down or even bring down the system. E-mail does not need to be synchronized with user registration, it can be made asynchronously and can be made into a separate job. At this time, mail delivery needs to be independent and can be made into a mail queue. As long as a mail message is sent to the mail queue when the user registers, the queue is responsible for the specific sending work.

Test the mail sender

There are several purposes for mail testing

The message is being processed, created, or sent. The content of the email is correct, including title, sender, body, etc. The right email was sent at the right time.

Messages can be tested in two directions, one is unit testing and the other is functional testing. In the unit test, we run the mail program in an isolated environment where the content is controlled, and then compare the output with the simulation data. In the functional test, we correctly used the mail sender in controller and model to test that the right mail was sent at the right time.

By default, the test environment cannot be emailed. They only add the message to the ActionMailer::Base.deliveries array, and by determining that the array is not empty, they can know whether the message was sent successfully or not.

Testing email is two things:

Test whether the message is queued. Test whether the content of the message is correct.

Test/unit/user_mailer_test.rb

Require 'test_helper'class UserMailerTest < ActionMailer::TestCaseinclude FactoryGirl::Syntax::Methodsdef test_registration_confirmationuser = FactoryGirl.create (: user_valid) mail = UserMailer.registration_confirmation (user). Deliverassert! ActionMailer::Base.deliveries.empty?assert_equal [user.email], mail.toend end

In the above test, first test whether the message queue is not empty, and then test whether the destination address of the message is correct.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report