module ActionMailer::TestHelper

def assert_emails(number)

end
end
ContactMailer.welcome.deliver
ContactMailer.welcome.deliver
assert_emails 2 do

end
ContactMailer.welcome.deliver
assert_emails 1 do
def test_emails_again

emails to be sent.
If a block is passed, that block should cause the specified number of

end
assert_emails 2
ContactMailer.welcome.deliver
assert_emails 1
ContactMailer.welcome.deliver
assert_emails 0
def test_emails

Asserts that the number of emails sent matches the given number.
def assert_emails(number)
  if block_given?
    original_count = ActionMailer::Base.deliveries.size
    yield
    new_count = ActionMailer::Base.deliveries.size
    assert_equal original_count + number, new_count, "#{number} emails expected, but #{new_count - original_count} were sent"
  else
    assert_equal number, ActionMailer::Base.deliveries.size
  end
end

def assert_no_emails(&block)

assert_emails 0

Note: This assertion is simply a shortcut for:

end
end
# No emails should be sent from this block
assert_no_emails do
def test_emails_again

If a block is passed, that block should not cause any emails to be sent.

end
assert_emails 1
ContactMailer.welcome.deliver
assert_no_emails
def test_emails

Assert that no emails have been sent.
def assert_no_emails(&block)
  assert_emails 0, &block
end