123456789_123456789_123456789_123456789_123456789_

Module: ActionMailer::TestCase::Behavior

Relationships & Source Files
Namespace Children
Modules:
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
self, Rails::Dom::Testing::Assertions::DomAssertions, Rails::Dom::Testing::Assertions::SelectorAssertions, ::ActionMailer::TestHelper, ::ActiveJob::TestHelper, ::ActiveSupport::Testing::Assertions, ::ActiveSupport::Testing::ConstantLookup
Defined in: actionmailer/lib/action_mailer/test_case.rb

Constant Summary

::ActiveSupport::Testing::Assertions - Included

UNTRACKED

Class Method Summary

::ActiveSupport::Concern - Extended

class_methods

Define class methods from given block.

included

Evaluate given block in context of base class, so that you can write class macros here.

prepended

Evaluate given block in context of base class, so that you can write class macros here.

append_features, prepend_features

Instance Attribute Summary

Instance Method Summary

::ActionMailer::TestHelper - Included

#assert_emails

Asserts that the number of emails sent matches the given number.

#assert_enqueued_email_with

Asserts that a specific email has been enqueued, optionally matching arguments and/or params.

#assert_enqueued_emails

Asserts that the number of emails enqueued for later delivery matches the given number.

#assert_no_emails

Asserts that no emails have been sent.

#assert_no_enqueued_emails

Asserts that no emails are enqueued for later delivery.

#capture_emails

Returns any emails that are sent in the block.

#deliver_enqueued_emails

Delivers all enqueued emails.

#delivery_job_filter

::ActiveJob::TestHelper - Included

#assert_enqueued_jobs

Asserts that the number of enqueued jobs matches the given number.

#assert_enqueued_with

Asserts that the job has been enqueued with the given arguments.

#assert_no_enqueued_jobs

Asserts that no jobs have been enqueued.

#assert_no_performed_jobs

Asserts that no jobs have been performed.

#assert_performed_jobs

Asserts that the number of performed jobs matches the given number.

#assert_performed_with

Asserts that the job has been performed with the given arguments.

#perform_enqueued_jobs

Performs all enqueued jobs.

#queue_adapter

Accesses the queue_adapter set by ::ActiveJob::Base.

#queue_adapter_for_test

Returns a queue adapter instance to use with all Active Job test helpers.

#clear_enqueued_jobs, #clear_performed_jobs, #deserialize_args_for_assertion, #enqueued_jobs_with, #filter_as_proc, #flush_enqueued_jobs, #instantiate_job, #jobs_with, #performed_jobs_with, #prepare_args_for_assertion, #queue_adapter_changed_jobs, #require_active_job_test_adapter!, #validate_option, #after_teardown, #before_setup

::ActiveSupport::Testing::Assertions - Included

#assert_changes

Assertion that the result of evaluating an expression is changed before and after invoking the passed in block.

#assert_difference

Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.

#assert_no_changes

Assertion that the result of evaluating an expression is not changed before and after invoking the passed in block.

#assert_no_difference

Assertion that the numeric result of evaluating an expression is not changed before and after invoking the passed in block.

#assert_not

Asserts that an expression is not truthy.

#assert_nothing_raised

Assertion that the block should not raise an exception.

#assert_raise
#assert_raises

Asserts that a block raises one of exp.

#_assert_nothing_raised_or_warn, #_callable_to_source_string

DSL Calls

included

[ GitHub ]


40
41
42
43
44
45
46
47
48
49
# File 'actionmailer/lib/action_mailer/test_case.rb', line 40

included do
  class_attribute :_decoders, default: Hash.new(->(body) { body }).merge!(
    Mime[:html] => ->(body) { Rails::Dom::Testing.html_document.parse(body) }
  ).freeze # :nodoc:
  class_attribute :_mailer_class
  setup :initialize_test_deliveries
  setup :set_expected_mail
  teardown :restore_test_deliveries
  ActiveSupport.run_load_hooks(:action_mailer_test_case, self)
end

Instance Method Details

#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
[ GitHub ]

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

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))
[ GitHub ]

  
# 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

#charset (private)

[ GitHub ]

  
# File 'actionmailer/lib/action_mailer/test_case.rb', line 165

def charset
  "UTF-8"
end

#encode(subject) (private)

[ GitHub ]

  
# File 'actionmailer/lib/action_mailer/test_case.rb', line 169

def encode(subject)
  Mail::Encodings.q_value_encode(subject, charset)
end

#initialize_test_deliveries (private)

[ GitHub ]

  
# File 'actionmailer/lib/action_mailer/test_case.rb', line 137

def initialize_test_deliveries
  set_delivery_method :test
  @old_perform_deliveries = ActionMailer::Base.perform_deliveries
  ActionMailer::Base.perform_deliveries = true
  ActionMailer::Base.deliveries.clear
end

#last_delivered_mail (private)

[ GitHub ]

  
# File 'actionmailer/lib/action_mailer/test_case.rb', line 173

def last_delivered_mail
  self.class.mailer_class.deliveries.last
end

#last_delivered_mail! (private)

[ GitHub ]

  
# File 'actionmailer/lib/action_mailer/test_case.rb', line 177

def last_delivered_mail!
  last_delivered_mail.tap do |mail|
    flunk "No e-mail in delivery list" if mail.nil?
  end
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

[ GitHub ]

  
# 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

#restore_delivery_method (private)

[ GitHub ]

  
# File 'actionmailer/lib/action_mailer/test_case.rb', line 154

def restore_delivery_method
  ActionMailer::Base.deliveries.clear
  ActionMailer::Base.delivery_method = @old_delivery_method
end

#restore_test_deliveries (private)

[ GitHub ]

  
# File 'actionmailer/lib/action_mailer/test_case.rb', line 144

def restore_test_deliveries
  restore_delivery_method
  ActionMailer::Base.perform_deliveries = @old_perform_deliveries
end

#set_delivery_method(method) (private)

[ GitHub ]

  
# File 'actionmailer/lib/action_mailer/test_case.rb', line 149

def set_delivery_method(method)
  @old_delivery_method = ActionMailer::Base.delivery_method
  ActionMailer::Base.delivery_method = method
end

#set_expected_mail (private)

[ GitHub ]

  
# File 'actionmailer/lib/action_mailer/test_case.rb', line 159

def set_expected_mail
  @expected = Mail.new
  @expected.content_type ["text", "plain", { "charset" => charset }]
  @expected.mime_version = "1.0"
end