Module: ActionMailer::TestCase::Behavior

Extended by:
ActiveSupport::Concern
Includes:
ActionMailer::TestHelper, ActiveSupport::Testing::ConstantLookup, Rails::Dom::Testing::Assertions::DomAssertions, Rails::Dom::Testing::Assertions::SelectorAssertions
Included in:
ActionMailer::TestCase
Defined in:
actionmailer/lib/action_mailer/test_case.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary

Constants included from ActiveSupport::Testing::Assertions

ActiveSupport::Testing::Assertions::UNTRACKED

Instance Method Summary collapse

Methods included from ActiveSupport::Concern

append_features, class_methods, extended, included, prepend_features, prepended

Methods included from ActionMailer::TestHelper

#assert_emails, #assert_enqueued_email_with, #assert_enqueued_emails, #assert_no_emails, #assert_no_enqueued_emails, #capture_emails, #deliver_enqueued_emails

Methods included from ActiveJob::TestHelper

#after_teardown, #assert_enqueued_jobs, #assert_enqueued_with, #assert_no_enqueued_jobs, #assert_no_performed_jobs, #assert_performed_jobs, #assert_performed_with, #before_setup, #perform_enqueued_jobs, #queue_adapter, #queue_adapter_for_test

Methods included from ActiveSupport::Testing::Assertions

#assert_changes, #assert_difference, #assert_no_changes, #assert_no_difference, #assert_not, #assert_nothing_raised, #assert_raises

Instance Method Details

#assert_no_part(content_type, mail = last_delivered_mail!) ) ⇒ Object

Assert that a Mail instance does not have a part with a matching MIME type

By default, assert against the last delivered Mail.

UsersMailer.create(user).deliver_now

assert_no_part :html
assert_no_part :text


129
130
131
132
133
134
# File 'actionmailer/lib/action_mailer/test_case.rb', line 129

def assert_no_part(content_type, mail = last_delivered_mail!)
  mime_type = Mime[content_type]
  part = [*mail.parts, mail].find { |part| mime_type.match?(part.mime_type) }

  assert_nil part, "expected no part matching #{mime_type} in #{mail.inspect}"
end

#assert_part(content_type, mail = last_delivered_mail!) ) {|decoder.call(part.decoded)| ... } ⇒ Object

Assert that a Mail instance has a part matching the content type. If the Mail is multipart, extract and decode the appropriate part. Yield the decoded part to the block.

By default, assert against the last delivered Mail.

UsersMailer.create(user).deliver_now
assert_part :text do |text|
  assert_includes text, "Welcome, #{user.email}"
end
assert_part :html do |html|
  assert_dom html.root, "h1", text: "Welcome, #{user.email}"
end

Assert against a Mail instance when provided

mail = UsersMailer.create(user)
assert_part :text, mail do |text|
  assert_includes text, "Welcome, #{user.email}"
end
assert_part :html, mail do |html|
  assert_dom html.root, "h1", text: "Welcome, #{user.email}"
end

Yields:

  • (decoder.call(part.decoded))


111
112
113
114
115
116
117
118
119
# File 'actionmailer/lib/action_mailer/test_case.rb', line 111

def assert_part(content_type, mail = last_delivered_mail!)
  mime_type = Mime[content_type]
  part = [*mail.parts, mail].find { |part| mime_type.match?(part.mime_type) }
  decoder = _decoders[mime_type]

  assert_not_nil part, "expected part matching #{mime_type} in #{mail.inspect}"

  yield decoder.call(part.decoded) if block_given?
end

#read_fixture(action) ⇒ Object

Reads the fixture file for the given mailer.

This is useful when testing mailers by being able to write the body of an email inside a fixture. See the testing guide for a concrete example: guides.rubyonrails.org/testing.html#revenge-of-the-fixtures



85
86
87
# File 'actionmailer/lib/action_mailer/test_case.rb', line 85

def read_fixture(action)
  IO.readlines(File.join(Rails.root, "test", "fixtures", self.class.mailer_class.name.underscore, action))
end