Module: Shoulda::ActionMailer::Assertions

Included in:
ActionController::Integration::Session, Test::Unit::TestCase
Defined in:
lib/shoulda/action_mailer/assertions.rb

Instance Method Summary collapse

Instance Method Details

#assert_did_not_send_emailObject

Asserts that no ActionMailer mails were delivered

assert_did_not_send_email

33
34
35
36
37
38
# File 'lib/shoulda/action_mailer/assertions.rb', line 33

def assert_did_not_send_email
  ::ActiveSupport::Deprecation.warn("use: should_not have_sent_email")
  msg = "Sent #{::ActionMailer::Base.deliveries.size} emails.\n"
  ::ActionMailer::Base.deliveries.each { |m| msg << "  '#{m.subject}' sent to #{m.to.to_sentence}\n" }
  assert ::ActionMailer::Base.deliveries.empty?, msg
end

#assert_sent_emailObject

Asserts that an email was delivered. Can take a block that can further narrow down the types of emails you're expecting.

assert_sent_email

Passes if ActionMailer::Base.deliveries has an email

assert_sent_email do |email|
  email.subject =~ /hi there/ && email.to.include?('[email protected]')
end

Passes if there is an email with subject containing 'hi there' and '[email protected]' as one of the recipients.


18
19
20
21
22
23
24
25
26
# File 'lib/shoulda/action_mailer/assertions.rb', line 18

def assert_sent_email
::ActiveSupport::Deprecation.warn("use: should have_sent_email")
  emails = ::ActionMailer::Base.deliveries
  assert !emails.empty?, "No emails were sent"
  if block_given?
    matching_emails = emails.select {|email| yield email }
    assert !matching_emails.empty?, "None of the emails matched."
  end
end