测试针对远程主机的ActionMailer交付

时间:2013-02-21 13:58:56

标签: ruby-on-rails cucumber capybara actionmailer

所以我有一个简单的场景,新注册的用户必须得到管理员的批准。经批准后,他们会收到邮件通知。然而,测试在最后一步失败了。这是cuke:

@javascript
Scenario: approving users
  Given user exists with email: "user@site.com", approved: false
    And I am on the admin panel
  When I approve the user user@site.com
  Then user should exist with email: "user@site.com", approved: true
    And the page should have no "approve" items
    And an email should have been sent to "user@site.com" with the subject "user_mailer.notify_approved.subject"

步骤定义(它是here)尝试在ActionMailer交付中查找邮件,但找不到它。

使测试失败的原因是,在我的测试设置中,我告诉Capybara不要运行服务器实例,而是连接到远程服务器(具有自签名证书的Thin)。这是设置:

config.use_transactional_fixtures = false

config.include Devise::TestHelpers, type: :controller
config.include FactoryGirl::Syntax::Methods

config.before(:suite) do
  DatabaseCleaner.strategy = :truncation
end

config.before(:each) do
  DatabaseCleaner.start
end

config.after(:each) do
  DatabaseCleaner.clean
end

# have to run the test env server separately, e.g. with thin:
# thin start -p 5678 --ssl -e test
Capybara.configure do |c|
  c.run_server = false
  c.server_port = 5678
  c.app_host = "https://localhost:%d" % c.server_port
end

由于邮件是从Capybara点击批准链接触发的远程测试服务器发送的,因此显然缺少交付:

When /^I approve the user (.*?)$/ do |email|
  page.find(:xpath, "//tr[descendant::a[text()='#{email}']]/td[@class='actions']//li[@class='approve']/a").click
end

所以问题是,是否有办法以某种方式判断邮件是否真的在这种情况下得到了传递。我能想到的一种方法是扩展上面的步骤,以便在本地更新相应的用户实例,这将在本地执行相同的代码,但这似乎闻到了。不使用SSL可能是另一个w / a,但我应该真正运行https。还有其他选择吗?

1 个答案:

答案 0 :(得分:0)

好的,因为没有答案,我就是这样解决的:

When /^I approve the user (.*?)$/ do |email|
  page.find(:xpath, "//tr[descendant::a[text()='#{email}']]/td[@class='actions']//li[@class='approve']/a").click

  u = User.find_by_email(email)
  u.approved = true
  u.save
end

添加的三行确保邮件通知回调也在本地触发。