【问题标题】:Testing with RSpec and Action Mailer: Clearing enqueued emails?使用 RSpec 和 Action Mailer 进行测试:清除排队的电子邮件?
【发布时间】:2019-02-13 01:58:58
【问题描述】:

Active Job Test Helper 提供了有用的方法 clear_enqueued_jobs。

Action Mailer 是否有类似的功能来清除排队的电子邮件?

【问题讨论】:

    标签: ruby-on-rails rspec actionmailer rails-activejob


    【解决方案1】:

    是的,您可以配置 rspec 以在每次测试之前清除邮件,例如:

    RSpec.configure do |config|
      config.before(:each) do
        ActionMailer::Base.deliveries.clear
      end
    end
    

    在排队邮件的情况下,Active Job Test Helper 包含在 ActiveMailer::TestHelper 中,因此您也应该能够使用clear_enqueued_jobs 方法。

    来源:https://github.com/rails/rails/blob/94b5cd3a20edadd6f6b8cf0bdf1a4d4919df86cb/actionmailer/lib/action_mailer/test_helper.rb#L9

    【讨论】:

    • 这会清除发送的电子邮件数量,而不是排队的电子邮件。你也可以写成 ActionMailer::Base.deliveries = []
    • @bagofcole 我已经更新了答案,如果可行,请告诉我。
    • 我已经确认它有效。如果那在文档中会很好:api.rubyonrails.org/classes/ActiveJob/TestHelper.html。 @JCorcuera 你应该考虑贡献!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 2013-05-21
    • 2014-04-06
    • 2013-04-14
    • 1970-01-01
    相关资源
    最近更新 更多