提供用于测试 Action Mailer 的辅助方法,包括 assert_emails
和 assert_no_emails
.
方法
- A
- C
- D
包含的模块
实例公共方法
assert_emails(number, &block) 链接
断言发送的邮件数量与给定数量匹配。
def test_emails
assert_emails 0
ContactMailer.welcome.deliver_now
assert_emails 1
ContactMailer.welcome.deliver_now
assert_emails 2
end
如果传递了一个块,则该块应该导致发送指定数量的邮件。
def test_emails_again
assert_emails 1 do
ContactMailer.welcome.deliver_now
end
assert_emails 2 do
ContactMailer.welcome.deliver_now
ContactMailer.welcome.deliver_later
end
end
来源:显示 | 在 GitHub 上
# File actionmailer/lib/action_mailer/test_helper.rb, line 35 def assert_emails(number, &block) if block_given? diff = capture_emails(&block).length assert_equal number, diff, "#{number} emails expected, but #{diff} were sent" else assert_equal number, ActionMailer::Base.deliveries.size end end
assert_enqueued_email_with(mailer, method, params: nil, args: nil, queue: nil, &block) 链接
断言已排队特定邮件,可以选择匹配参数和/或参数。
def test_email
ContactMailer.welcome.deliver_later
assert_enqueued_email_with ContactMailer, :welcome
end
def test_email_with_parameters
ContactMailer.with(greeting: "Hello").welcome.deliver_later
assert_enqueued_email_with ContactMailer, :welcome, args: { greeting: "Hello" }
end
def test_email_with_arguments
ContactMailer.welcome("Hello", "Goodbye").deliver_later
assert_enqueued_email_with ContactMailer, :welcome, args: ["Hello", "Goodbye"]
end
def test_email_with_named_arguments
ContactMailer.welcome(greeting: "Hello", farewell: "Goodbye").deliver_later
assert_enqueued_email_with ContactMailer, :welcome, args: [{ greeting: "Hello", farewell: "Goodbye" }]
end
def test_email_with_parameters_and_arguments
ContactMailer.with(greeting: "Hello").welcome("Cheers", "Goodbye").deliver_later
assert_enqueued_email_with ContactMailer, :welcome, params: { greeting: "Hello" }, args: ["Cheers", "Goodbye"]
end
def test_email_with_parameters_and_named_arguments
ContactMailer.with(greeting: "Hello").welcome(farewell: "Goodbye").deliver_later
assert_enqueued_email_with ContactMailer, :welcome, params: { greeting: "Hello" }, args: [{farewell: "Goodbye"}]
end
def test_email_with_parameterized_mailer
ContactMailer.with(greeting: "Hello").welcome.deliver_later
assert_enqueued_email_with ContactMailer.with(greeting: "Hello"), :welcome
end
def test_email_with_matchers
ContactMailer.with(greeting: "Hello").welcome("Cheers", "Goodbye").deliver_later
assert_enqueued_email_with ContactMailer, :welcome,
params: ->(params) { /hello/i.match?(params[:greeting]) },
args: ->(args) { /cheers/i.match?(args[0]) }
end
如果传递了一个块,则该块应该导致发送指定数量的邮件。
def test_email_in_block
assert_enqueued_email_with ContactMailer, :welcome do
ContactMailer.welcome.deliver_later
end
end
如果 args
作为 Hash
提供,则会匹配参数化的邮件。
def test_parameterized_email
assert_enqueued_email_with ContactMailer, :welcome,
args: {email: '[email protected]'} do
ContactMailer.with(email: '[email protected]').welcome.deliver_later
end
end
来源:显示 | 在 GitHub 上
# File actionmailer/lib/action_mailer/test_helper.rb, line 157 def assert_enqueued_email_with(mailer, method, params: nil, args: nil, queue: nil, &block) if mailer.is_a? ActionMailer::Parameterized::Mailer params = mailer.instance_variable_get(:@params) mailer = mailer.instance_variable_get(:@mailer) end args = Array(args) unless args.is_a?(Proc) queue ||= mailer.deliver_later_queue_name || ActiveJob::Base.default_queue_name expected = ->(job_args) do job_kwargs = job_args.extract_options! [mailer.to_s, method.to_s, "deliver_now"] == job_args && params === job_kwargs[:params] && args === job_kwargs[:args] end assert_enqueued_with(job: mailer.delivery_job, args: expected, queue: queue.to_s, &block) end
assert_enqueued_emails(number, &block) 链接
断言排队以供以后传递的邮件数量与给定数量匹配。
def test_emails
assert_enqueued_emails 0
ContactMailer.welcome.deliver_later
assert_enqueued_emails 1
ContactMailer.welcome.deliver_later
assert_enqueued_emails 2
end
如果传递了一个块,则该块应该导致发送指定数量的邮件。
def test_emails_again
assert_enqueued_emails 1 do
ContactMailer.welcome.deliver_later
end
assert_enqueued_emails 2 do
ContactMailer.welcome.deliver_later
ContactMailer.welcome.deliver_later
end
end
来源:显示 | 在 GitHub 上
# File actionmailer/lib/action_mailer/test_helper.rb, line 91 def assert_enqueued_emails(number, &block) assert_enqueued_jobs(number, only: ->(job) { delivery_job_filter(job) }, &block) end
assert_no_emails(&block) 链接
断言没有邮件被发送。
def test_emails
assert_no_emails
ContactMailer.welcome.deliver_now
assert_emails 1
end
如果传递了一个块,则该块不应导致发送任何邮件。
def test_emails_again
assert_no_emails do
# No emails should be sent from this block
end
end
注意:此断言只是 assert_emails(0, &block)
的快捷方式。
assert_emails 0, &block
来源:显示 | 在 GitHub 上
# File actionmailer/lib/action_mailer/test_helper.rb, line 63 def assert_no_emails(&block) assert_emails 0, &block end
assert_no_enqueued_emails(&block) 链接
断言没有邮件排队以供以后传递。
def test_no_emails
assert_no_enqueued_emails
ContactMailer.welcome.deliver_later
assert_enqueued_emails 1
end
如果提供了一个块,则它不应导致发送任何邮件。
def test_no_emails
assert_no_enqueued_emails do
# No emails should be enqueued from this block
end
end
来源:显示 | 在 GitHub 上
# File actionmailer/lib/action_mailer/test_helper.rb, line 191 def assert_no_enqueued_emails(&block) assert_enqueued_emails 0, &block end
capture_emails(&block) 链接
返回在块中发送的任何邮件。
def test_emails
emails = capture_emails do
ContactMailer.welcome.deliver_now
end
assert_equal "Hi there", emails.first.subject
emails = capture_emails do
ContactMailer.welcome.deliver_now
ContactMailer.welcome.deliver_later
end
assert_equal "Hi there", emails.first.subject
end
来源:显示 | 在 GitHub 上
# File actionmailer/lib/action_mailer/test_helper.rb, line 249 def capture_emails(&block) original_count = ActionMailer::Base.deliveries.size deliver_enqueued_emails(&block) new_count = ActionMailer::Base.deliveries.size diff = new_count - original_count ActionMailer::Base.deliveries.last(diff) end
deliver_enqueued_emails(queue: nil, at: nil, &block) 链接
传递所有排队的邮件。如果给出了一个块,则传递在块持续时间内排队的邮件。如果没有给出块,则传递到测试中这一点为止的排队邮件。
def test_deliver_enqueued_emails
deliver_enqueued_emails do
ContactMailer.welcome.deliver_later
end
assert_emails 1
end
def test_deliver_enqueued_emails_without_block
ContactMailer.welcome.deliver_later
deliver_enqueued_emails
assert_emails 1
end
如果指定了 :queue
选项,则只执行排队到特定队列的邮件(s)。
def test_deliver_enqueued_emails_with_queue
deliver_enqueued_emails queue: :external_mailers do
CustomerMailer.deliver_later_queue_name = :external_mailers
CustomerMailer.welcome.deliver_later # will be performed
EmployeeMailer.deliver_later_queue_name = :internal_mailers
EmployeeMailer.welcome.deliver_later # will not be performed
end
assert_emails 1
end
如果指定了 :at
选项,则只传递排队以立即传递或在给定时间之前传递的邮件。
来源:显示 | 在 GitHub 上
# File actionmailer/lib/action_mailer/test_helper.rb, line 231 def deliver_enqueued_emails(queue: nil, at: nil, &block) perform_enqueued_jobs(only: ->(job) { delivery_job_filter(job) }, queue: queue, at: at, &block) end