使用水豚和delayed_job测试电子邮件

发布于 2024-12-02 14:49:58 字数 434 浏览 1 评论 0原文

受到 Railscast (http://railscasts.com/episodes/275-how-i-test) 剧集的启发,我尝试向我的应用程序添加一些请求规范。

使用delayed_job发送电子邮件,我没有找到一种简单的方法来在水豚测试中测试电子邮件的发送。我尝试过:

    it "emails user when requesting password reset" do
      ...(some user action that triggers sending an email)...
      Delayed::Worker.new.work_off
      ActionMailer::Base.deliveries.last.to.should include(user.email)
    end

感谢您的任何提示或帮助!

inspired by the episode of railscast (http://railscasts.com/episodes/275-how-i-test) i tried to add some request specs to my app.

using delayed_job for sending my emails, i did not found an easy way to test the sending of emails within my capybara test. i tried:

    it "emails user when requesting password reset" do
      ...(some user action that triggers sending an email)...
      Delayed::Worker.new.work_off
      ActionMailer::Base.deliveries.last.to.should include(user.email)
    end

thanks for any hints or help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月打碎记忆 2024-12-09 14:49:58

更简单的解决方案:只需在 config/initializers 中创建一个初始化程序,例如delayed_job_config.rb

Delayed::Worker.delay_jobs = !Rails.env.test?

在实际测试中,您甚至不需要知道已部署delayed_job 来发送邮件,这使它们保持干净和简单。

Much easier solution: Just create an initializer in config/initializers, e.g. delayed_job_config.rb

Delayed::Worker.delay_jobs = !Rails.env.test?

In your actual test you do not even need to know that delayed_job is deployed to send the mails, which keeps them clean and simple.

梦一生花开无言 2024-12-09 14:49:58

嗯,应该可以。我在我的应用程序中发现了一些错误配置。

检查你的测试环境。你应该设置:

Staffomatic::Application.configure do
  ...
  config.action_mailer.delivery_method = :test
  config.action_mailer.default_url_options = { :host => "example.com" }
end

我在railscast的邮件宏模块中添加了一行来处理Delayed::Job:

module MailerMacros
  def last_email
    Delayed::Worker.new.work_off
    ActionMailer::Base.deliveries.last
  end

  def reset_email
    Delayed::Job.destroy_all
    ActionMailer::Base.deliveries = []
  end
end

现在你可以检查:

last_email.to.should include(user.email)

你的最后一封电子邮件。

很容易!

附:如果您安装了很棒的 mail_safe gem,您应该确保它不在测试环境中!

well, it should work. i found some misconfigurations in my app.

check you test env. you should set:

Staffomatic::Application.configure do
  ...
  config.action_mailer.delivery_method = :test
  config.action_mailer.default_url_options = { :host => "example.com" }
end

i added a line in the mailer macros module from the railscast to work off the Delayed::Job:

module MailerMacros
  def last_email
    Delayed::Worker.new.work_off
    ActionMailer::Base.deliveries.last
  end

  def reset_email
    Delayed::Job.destroy_all
    ActionMailer::Base.deliveries = []
  end
end

now you can check with:

last_email.to.should include(user.email)

your last email.

pretty easy!

ps. if you have the awesome mail_safe gem installed you should make sure it's not in the test env.!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文