简体   繁体   中英

How to test rails mailers with selenium

I am attempting to write a selenium/capybara test that clicks on a button generating an email and then verifies that email has been sent. I need to be able to access the contents of the email as well.

For example, when clicking on the devise "Forgot your password?" link, and then submitting the form, an email is sent. I see the email in my server logs:

11:19:00 web.1    | [127.0.0.1] [60db47a6946763b2fe5ec5c34be2b8f3]   Rendered devise/mailer/reset_password_instructions.html.erb (1.7ms)
11:19:00 web.1    | [127.0.0.1] [60db47a6946763b2fe5ec5c34be2b8f3] 
11:19:00 web.1    | Sent mail to owner@MyApp.com (38.7ms)
11:19:00 web.1    | [127.0.0.1] [60db47a6946763b2fe5ec5c34be2b8f3] Date: Tue, 12 Aug 2014 11:19:00 -0700
11:19:00 web.1    | From: MyApp <messages@MyApp.com>
11:19:00 web.1    | Reply-To: MyApp <messages@MyApp.com>
11:19:00 web.1    | To: owner@MyApp.com
11:19:00 web.1    | Message-ID: <53ea5a9493c4f_827b3fc125033bdc91470@Gregs-MacBook-Air.local.mail>
11:19:00 web.1    | Subject: MyApp - Password Reset Instructions
11:19:00 web.1    | Mime-Version: 1.0
11:19:00 web.1    | Content-Type: text/html;
11:19:00 web.1    |  charset=UTF-8
11:19:00 web.1    | Content-Transfer-Encoding: 7bit
11:19:00 web.1    | X-MC-Track: opens
11:19:00 web.1    | X-MC-Tags: devise_reset_password_instructions
11:19:00 web.1    | 
11:19:00 web.1    | <p>Hello owner@MyApp.com!</p>
11:19:00 web.1    | 
11:19:00 web.1    | <p>Someone has requested a link to change your password. You can do this through the link below.</p>
11:19:00 web.1    | 
11:19:00 web.1    | <p><a href="http://192.168.11.44:3000/users/password/edit?reset_password_token=wh3rTHUH6KctsHxhxxqy">Change my password</a></p>
11:19:00 web.1    | 
11:19:00 web.1    | <p>If you didn't request this, please ignore this email.</p>
11:19:00 web.1    | <p>Your password won't change until you access the link above and create a new one.</p>

How can I access this from within my test?

In app/mailers I have two files.

my_mailer.rb :

class MyMailer < Devise::Mailer
  def headers_for(action, opts)
    headers = {
        :subject       => subject_for(action),
        :to            => resource.email,
        :from          => mailer_sender(devise_mapping),
        :reply_to      => mailer_reply_to(devise_mapping),
        :template_path => "devise/mailer",
        :template_name => action,
        "X-MC-Track"   => "opens",
        "X-MC-Tags"    => "devise_#{action}"
    }.merge(opts)
  end
end

and message.rb :

class Message < ActionMailer::Base
  require 'mandrill'
  default from: "MyApp <messages@MyApp.com>"

  def send_welcome_email(to_user)
    mandrill = Mandrill::API.new ENV['MANDRILL_API_KEY']
    template = mandrill.templates.render "welcome-to-MyApp", [], [
      {:name => 'username', :content => to_user.name},
      {:name => 'subject', :content => "Welcome to MyApp, #{to_user.name}"}
    ]
    @body = template['html']
    mail(:subject => "Welcome to MyApp, #{to_user.name}", :to => to_user.email, "tags" => ["In Trial - Welcome"],)
    headers['X-MC-Track'] = "opens"
    headers['X-MC-Tags'] ="invite_sent"
  end
end

You don't.

Just write a separate mailer spec.

Tests are like mathematical proofs:

Given user submits mail sending form, no error occurs and 'Mail was submitted' is displayed.
Given your Mailer is generating proper content.
Given Rails Mailers work
Given the Mail protocol works 
Then the mail will be delivered properly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM