2013-02-21 48 views
0

所以我有一个简单的情况下,新注册的用户必须通过管理员批准。经批准后,他们会收到邮件通知。然而,测试在最后一步失败。这里的黄瓜:测试ActionMailer交付对远程主机

@javascript 
Scenario: approving users 
    Given user exists with email: "[email protected]", approved: false 
    And I am on the admin panel 
    When I approve the user [email protected] 
    Then user should exist with email: "[email protected]", approved: true 
    And the page should have no "approve" items 
    And an email should have been sent to "[email protected]" 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 

交货显然是缺失的,因为邮件从远程测试服务器发送的,由水豚触发点击的批准链接:

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

回答

0

OK所以对于缺失的问题,这里是我如何解决它:

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 

的三行加入保证了邮件通知回调也是本地触发。