如何使用email_spec gem和cucumber测试Devise电子邮件的内容?

时间:2015-06-10 14:59:50

标签: ruby-on-rails email devise cucumber capybara

我正在学习如何使用黄瓜和电子邮件规范测试电子邮件生成和交付。

我已经安装了gem并生成了 email_steps.rb 文件。

然而 ActionMailer :: Base.deliveries 数组似乎总是空的,所以我无法测试该电子邮件。

我的方案如下:

Background:
    Given I visit the User Registration page

  Scenario: Happy Path - Seccessful Sign Up
    And I fill in the User Registration form correctly
    And I click Register
    Then I am redirected to the Users index page
    And I can see email confirmation notification
    And "John89@example.com" should receive an email 
    When I open the email with subject "Confirmation instructions"
    And I follow "Confirm my account" in the email
    And I am redirected to the Sign In page
    And I fill in the Sign In form correctly
    And I click Log In
    Then I am redirected to the Home page
    And I can see "Signed in successfully" notification

失败了
And "John89@example.com" should receive an email with subject "Confirmation instructions"

我已经检查过电子邮件是在开发环境中使用开信刀宝石发送的,但我不知道为什么它在测试环境中找不到

Then /^(?:I|they|"([^"]*?)") should receive (an|no|\d+) emails?$/ do |address, amount|
  unread_emails_for(address).size.should == parse_email_count(amount)
end

从email-spec gem开始。

我缺少一些配置设置吗?

1 个答案:

答案 0 :(得分:1)

问题在于我设置了

  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

开发环境中但不在测试环境中,这意味着在测试环境中发送的电子邮件出错了虽然在开发环境中手动执行它似乎很好。

这是主要问题,第二个小问题是我的电子邮件地址中有一个大写字母,在发送时会下降。这意味着当email-spec gem查找电子邮件时,它无法找到匹配项,因为它们并不完全匹配(deliveryies数组中的那个是低级的)。

调试此方法的一个好方法是设置黄瓜调试页面的步骤定义,如下所示:

Then(/^show me the page$/) do
  save_and_open_page
end

这使您可以在浏览器中查看当前页面,该页面允许我查看错误消息并找出问题。

如果有人在将来遇到同样的事情,我希望这会有所帮助