【问题标题】:Rspec Testing SMTP ConnectionRspec 测试 SMTP 连接
【发布时间】:2014-11-20 17:26:06
【问题描述】:

使用 RSpec,我想测试我的应用程序连接到我的 SMTP 服务器的能力,而无需(必须)传递任何消息。

如何打开与 SMTP 电子邮件服务器的连接(通过登录)并测试/查看收到的响应?

【问题讨论】:

  • 您可以使用一些邮件模拟 gem(如模拟邮件程序),或者您可以考虑使用mailcatcher.me 进行更类似于系统的测试。
  • 谢谢。我认为 mailcatcher 可能完成了我希望通过测试涵盖的大部分内容。为了使我的测试更加精细,我希望测试电子邮件创建过程的元素,包括验证 smtp 设置。我知道我可以在配置中将我的 smtp 设置从 test 切换到 smtp,但这会使测试比我想要的更脆弱,或者至少是一个只有在你必须记住要切换的其他设置的情况下才能工作的测试。跨度>
  • 按照惯例,配置(包括 SMTP 配置)在文件中声明(例如application.yml),然后分解为阶段(developmenttest、@ 987654326@...) 所以你不必切换任何东西,并且测试并不脆弱 - 测试你的配置不应该是测试你的功能的一部分......另见blog.rubybestpractices.com/posts/gregory/…

标签: ruby ruby-on-rails-4 rspec smtp actionmailer


【解决方案1】:
smtp = Net::SMTP.new 'mail.example.com', example_port
# smtp.enable_starttls_auto # uncomment this if starttls is needed
# smtp.enable_tls           # uncomment this if ssl/tls is needed but starttls is not supported
smtp.start('mydomain.example.com') do
  expect{ smtp.authenticate 'me@mydomain.example.com', 'mypassword', 'plain' }.to_not raise_error
end

如果身份验证失败,调用authenticate 将引发Net::SMTPAuthenticationError

否则,它将返回一个Net::SMTP::Response,在响应上调用status 将返回"235"

【讨论】:

    猜你喜欢
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多