Method: ActionMailer::TestHelper#assert_emails
- Defined in:
- actionmailer/lib/action_mailer/test_helper.rb
#assert_emails(number, &block) ⇒ Object
Asserts that the number of emails sent matches the given number.
def test_emails
assert_emails 0
ContactMailer.welcome.deliver_now
assert_emails 1
ContactMailer.welcome.deliver_now
assert_emails 2
end
If a block is passed, that block should cause the specified number of emails to be sent.
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
The method returns the Mail::Messages that were processed, enabling further analysis.
def test_emails_more_thoroughly
email = assert_emails 1 do
ContactMailer.welcome.deliver_now
end
assert_equal "Hi there", email.subject
emails = assert_emails 2 do
ContactMailer.welcome.deliver_now
ContactMailer.welcome.deliver_later
end
assert_equal "Hi there", emails.first.subject
end
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'actionmailer/lib/action_mailer/test_helper.rb', line 50 def assert_emails(number, &block) if block_given? original_count = ActionMailer::Base.deliveries.size deliver_enqueued_emails(&block) new_count = ActionMailer::Base.deliveries.size diff = new_count - original_count assert_equal number, diff, "#{number} emails expected, but #{diff} were sent" if diff == 1 ActionMailer::Base.deliveries.last else ActionMailer::Base.deliveries.last(diff) end else assert_equal number, ActionMailer::Base.deliveries.size end end |