0

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

@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"

步骤定义(在此处)尝试在 ActionMailer 传递中查找邮件但找不到。

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

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 运行。还有其他选择吗?

4

1 回答 1

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

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

于 2013-02-24T06:49:33.807 回答