测试针对远程主机的ActionMailer交付(testing ActionMailer deliveries against a remote host)

编程入门 行业动态 更新时间:2024-10-24 01:52:32
测试针对远程主机的ActionMailer交付(testing ActionMailer deliveries against a remote host)

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

步骤定义(它在这里 )尝试在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。 还有其他选择吗?

So I have a simple scenario where a newly registered user must be approved by an admin. Upon approval, they receive a mail notification. The test, however, fails at the last step. Here's the 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"

The step definition (it's here) tries to look up the mail in ActionMailer deliveries and can't find it.

What makes the test fail is that in my test setup, I tell Capybara not to run a server instance but connect to a remote server (Thin with a self-signed cert). Here's the setup:

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

The delivery is missing apparently because the mail is sent from the remote test server, triggered by Capybara clicking an approve link:

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

So the question is if there is a way to somehow tell if the mail really got delivered in this scenario. One way I can think of is extend the step above to also update the corresponding user instance locally which would execute the same code locally, but that seems to smell. Not using SSL could be another w/a, but I should really run over https. Are there any other options?

最满意答案

好的,因为缺乏答案,这就是我如何解决它:

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

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

OK so for the lack of an answer, here's how I solved it:

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

The three lines added ensure the mail notification callback is also triggered locally.

更多推荐

本文发布于:2023-08-01 16:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1361441.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主机   测试   ActionMailer   testing   remote

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!