我试图编写一个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
发布于 2014-08-13 08:14:18
你没有。
只需写一个单独的邮件规范。
考试就像数学证明:
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
https://stackoverflow.com/questions/25271869
复制相似问题