如何用硒测试铁路邮件

时间:2014-08-12 18:48:12

标签: ruby-on-rails email selenium devise capybara

我正在尝试编写selenium / capybara测试,点击生成电子邮件的按钮,然后验证是否已发送电子邮件。我也需要能够访问电子邮件的内容。

例如,点击设备“忘记密码?”链接,然后提交表单,发送电子邮件。我在服务器日志中看到了电子邮件:

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>

如何从我的测试中访问它?

app/mailers我有两个文件。

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

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

1 个答案:

答案 0 :(得分:0)

你没有。

只需编写单独的邮件规范。

测试就像数学证明:

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