如何访问使用letter_opener gem打开的电子邮件中的链接

时间:2016-06-17 18:39:13

标签: ruby cucumber capybara

我正在使用Cucumber和Capybara进行一些QA自动化,并采取措施:

When(/^I follow the reset password link in the email$/) do
  last_email = ActionMailer::Base.deliveries.last
  password_reset_url = last_email.body.match(/http.*\/users\/password\/edit.*$/)
  visit password_reset_url
end

步骤失败:

undefined method `body' for nil:NilClass (NoMethodError)

此外,在第一行之后删除binding.pry会导致last_email为零,这很奇怪。

有没有人对这里可能发生的事情提出建议或想法?

2 个答案:

答案 0 :(得分:3)

如果您正在使用letter_opener,那么电子邮件不会进入ActionMailer交付,而是在不受Capybara控制的浏览器中打开。如果你想在capybara中处理电子邮件,你应该看看capybara-email gem而不是letter_opener。 Letter_opener更多地针对开发环境,而不是测试

答案 1 :(得分:0)

Tom Walpole's answer内容绝对正确,并指向capybara-email gem的方向。

以下是您可以更改代码示例的一个潜在示例,假设您有另一个Cucumber步骤,单击按钮/链接以发送重置密码说明:

When(/^I follow the reset password link in the email (.*)$/) do |email|
  # finds the last email sent to the passed in email address.
  # This also sets the `current_email` object, which you can think
  # of as similar to Capybara's `page` object, but for an email.
  open_email(email)
  # Assuming the email link to change your password is called
  # 'Change my password', you can just click it, just as you would
  # on a Capybara `page`.
  current_email.click_link 'Change my password'
end

点击链接后,您将被带到page,您可以继续fill_in 'New password', with: 'foobar'等等,我假设您已经接受了另一个Cucumber步骤。