【问题标题】:How to test rails mailers with selenium如何使用 selenium 测试 Rails 邮件程序
【发布时间】:2014-10-05 23:15:40
【问题描述】:

我正在尝试编写一个 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

【问题讨论】:

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


    【解决方案1】:

    你没有。

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

    测试就像数学证明:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-02
      • 2013-02-20
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 2016-05-18
      相关资源
      最近更新 更多