3

I can't figure out how to get the syntax to work for asserting that an email has been enqueued properly. Specifically, how do you pass in the arguments to the mailer when the argument is an object?

For example, if you want to pass in a contact object to the example from the documentation:

  assert_enqueued_email_with ContactMailer, :welcome, @contact do
    ContactMailer.welcome(@contact).deliver_later
  end

  assert_enqueued_email_with ContactMailer, :welcome, args: @contact do
    ContactMailer.welcome.deliver_later
  end

  assert_enqueued_email_with ContactMailer, :welcome, [@contact] do
    ContactMailer.welcome.deliver_later
  end

None of these appear to work. If you look at the actual enqueued job, the object looks like this:

{:job=>ActionMailer::DeliveryJob, :args=>["ContactMailer", "welcome", "deliver_now", {"_aj_globalid"=>"gid://app_name/Contact/1015983224"}], :queue=>"mailers"}

Any help is appreciated!

4

4 回答 4

3

您需要传递一组参数:

assert_enqueued_email_with ContactMailer, :welcome, args: [@contact] do
  ContactMailer.welcome(@contact).deliver_later
end
于 2019-02-26T14:17:33.833 回答
2

我知道这可能为时已晚,无法帮助您,但我最近遇到了同样的问题,所以希望这个解决方法能帮助下一个人。在后台,assert_enqueued_email_with只使用了 assert_enqueued_with,我们可以直接使用它来创建一个可以像这样查找入队电子邮件的测试。

assert_enqueued_with(job: ContactMailer.delivery_job, args: ['ContactMailer', 'welcome', 'deliver_now', @contact], queue: 'mailers')

此测试与 assert_enqueued_email_with 生成的唯一区别是 @contact 没有包装在额外的数组中。

编辑:我想回来提一下,我只在从 Rails 5.2 更新到 Rails 6.0 的过程中遇到了这个错误。一旦我进一步了解更新,它就消失了,assert_enqueued_with 按预期工作,而之前使用 assert_enqueued_with 的解决方法不再匹配。特别是在将 delivery_job 设置为新的默认值之后。

Rails.application.config.action_mailer.delivery_job = "ActionMailer::MailDeliveryJob"
于 2019-09-04T22:13:03.100 回答
0

我遇到了同样的问题,所以我的解决方案是直接访问排队的作业数组并手动进行比较。

assert_equal ActiveJob::Base.queue_adapter.enqueued_jobs[0][:args][0], "UserMailer"
assert_equal ActiveJob::Base.queue_adapter.enqueued_jobs[1][:args][0], "MarketingMailer"
于 2021-04-30T10:09:41.907 回答
-1

您可以尝试指定等待时间

Notifier.welcome(User.first).deliver_later(wait: 1.hour)

Deliver_later 的更多选项:

http://api.rubyonrails.org/classes/ActionMailer/MessageDelivery.html#method-i-deliver_later

于 2018-04-07T17:47:59.913 回答