测试电子邮件发送黄瓜

时间:2013-02-06 17:46:54

标签: ruby-on-rails-3 email cucumber actionmailer

我有一个在Cucumber中分配给我的故事列表,其中一个是“然后用户应该收到确认电子邮件”。我认为用户收到它的测试超出了应用程序的功能,但我如何测试刚刚发送的电子邮件?

5 个答案:

答案 0 :(得分:7)

您可以使用此步骤定义:

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进行测试

Source

答案 1 :(得分:2)

email_spec + action_mailer_cache_delivery宝石是您做这件事的朋友

答案 2 :(得分:0)

我建议你在一些动作之后验证last_response,例如,用户点击某个按钮,或类似的事情。

或者,如果您在执行某项操作后更新记录,请检查updated_at属性以查看其是否已更改。

答案 3 :(得分:0)

检查dockyard/capybara-email gem:

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

答案 4 :(得分:0)

另一个选项是PutsBox。您可以发送电子邮件至whatever-you-want@putsbox.com,等待几秒钟(SMTP内容即时),然后通过http://preview.putsbox.com/p/whatever-you-want/last查看您的电子邮件。

这个post tutorial有一些例子。