【发布时间】:2013-01-22 00:44:23
【问题描述】:
我在 Cucumber 中有一个分配给我的故事列表,其中之一是“那么用户应该收到一封确认电子邮件”。我认为测试用户是否收到它超出了应用程序的能力,但是我如何测试一封电子邮件是否刚刚发送?
【问题讨论】:
标签: ruby-on-rails-3 email cucumber actionmailer
我在 Cucumber 中有一个分配给我的故事列表,其中之一是“那么用户应该收到一封确认电子邮件”。我认为测试用户是否收到它超出了应用程序的能力,但是我如何测试一封电子邮件是否刚刚发送?
【问题讨论】:
标签: ruby-on-rails-3 email cucumber actionmailer
我建议您在某些操作发生后验证last_response,例如用户单击按钮或类似的操作。
或者,如果您在执行某项操作后更新记录,请检查 updated_at 属性以查看它是否已更改。
【讨论】:
你可以使用这个步骤定义:
Then "the user should receive a confirmation email" do
# this will get the first email, so we can check the email headers and body.
email = ActionMailer::Base.deliveries.first
email.from.should == "admin@example.com"
email.to.should == @user.email
email.body.should include("some key word or something....")
end
使用 Rails 3.2 测试
【讨论】:
email_spec + action_mailer_cache_delivery gems 是你这样做的朋友
【讨论】:
查看dockyard/capybara-emailgem:
feature 'Emailer' do
background do
# will clear the message queue
clear_emails
visit email_trigger_path
# Will find an email sent to test@example.com
# and set `current_email`
open_email('test@example.com')
end
scenario 'following a link' do
current_email.click_link 'your profile'
expect(page).to have_content 'Profile page'
end
scenario 'testing for content' do
expect(current_email).to have_content 'Hello Joe!'
end
scenario 'testing for a custom header' do
expect(current_email.headers).to include 'header-key'
end
scenario 'testing for a custom header value' do
expect(current_email.header('header-key')).to eq 'header_value'
end
scenario 'view the email body in your browser' do
# the `launchy` gem is required
current_email.save_and_open
end
end
【讨论】:
另一个选项是PutsBox。您可以发送电子邮件至whatever-you-want@putsbox.com,等待几秒钟(SMTP 内容不是即时的),然后通过http://preview.putsbox.com/p/whatever-you-want/last 查看您的电子邮件。
这个post tutorial 有一些例子。
【讨论】: