我在Cucumber中有一个故事列表,其中一个是"然后用户应该收到一封确认电子邮件"。我认为测试用户是否收到电子邮件超出了应用程序的能力,但我如何测试电子邮件是否刚刚发送?
您可以使用以下步骤定义:
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_dedelivery宝石是您的好友
我建议您在一些操作之后验证last_response
,比如用户单击按钮或类似的操作。
或者,如果您在执行某些操作后更新记录,请检查updated_at
属性以查看它是否已更改。
查看造船厂/水豚的电子邮件宝石:
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.
这篇文章教程有一些例子。