Method: Rails::Dom::Testing::Assertions::SelectorAssertions#assert_dom_email

Defined in:
lib/rails/dom/testing/assertions/selector_assertions.rb

#assert_dom_email(html_version: nil, &block) ⇒ Object Also known as: assert_select_email

Extracts the body of an email and runs nested assertions on it.

You must enable deliveries for this assertion to work, use:

ActionMailer::Base.perform_deliveries = true

Example usage:

assert_dom_email do
  assert_dom "h1", "Email alert"
end

assert_dom_email do
  items = assert_dom "ol>li"
  items.each do
     # Work with items here...
  end
end

The DOM is created using an HTML parser specified by Rails::Dom::Testing.default_html_version (either :html4 or :html5).

When testing in a Rails application, the parser default can also be set by setting Rails.application.config.dom_testing_default_html_version.

If you want to specify the HTML parser just for a particular assertion, pass html_version: :html4 or html_version: :html5 keyword arguments:

assert_dom_email(html_version: :html5) do
  assert_dom "h1", "Email alert"
end

285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/rails/dom/testing/assertions/selector_assertions.rb', line 285

def assert_dom_email(html_version: nil, &block)
  deliveries = ActionMailer::Base.deliveries
  assert !deliveries.empty?, "No e-mail in delivery list"

  deliveries.each do |delivery|
    (delivery.parts.empty? ? [delivery] : delivery.parts).each do |part|
      if /^text\/html\W/.match?(part["Content-Type"].to_s)
        root = Rails::Dom::Testing.html_document_fragment(html_version: html_version).parse(part.body.to_s)
        assert_dom root, ":root", &block
      end
    end
  end
end