Skip to Content Skip to Search
Namespace
Methods
A
R
Included Modules

Instance Public methods

assert_no_part(content_type, mail = last_delivered_mail!)

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
# 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!)

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
# 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)

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

# 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