Module: Rails::Dom::Testing::Assertions::DomAssertions

Included in:
Rails::Dom::Testing::Assertions
Defined in:
lib/rails/dom/testing/assertions/dom_assertions.rb

Instance Method Summary collapse

Instance Method Details

#assert_dom_equal(expected, actual, message = nil, strict: false, html_version: nil) ⇒ Object

Test two HTML strings for equivalency (e.g., equal even when attributes are in another order)

# assert that the referenced method generates the appropriate HTML string
assert_dom_equal(
  '<a href="http://www.example.com">Apples</a>',
  link_to("Apples", "http://www.example.com"),
)

By default, the matcher will not pay attention to whitespace in text nodes (e.g., spaces and newlines). If you want stricter matching with exact matching for whitespace, pass strict: true:

# these assertions will both pass
assert_dom_equal     "<div>\nfoo\n\</div>", "<div>foo</div>", strict: false
assert_dom_not_equal "<div>\nfoo\n\</div>", "<div>foo</div>", strict: true

The DOMs are 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_equal expected, actual, html_version: :html5


35
36
37
38
39
# File 'lib/rails/dom/testing/assertions/dom_assertions.rb', line 35

def assert_dom_equal(expected, actual, message = nil, strict: false, html_version: nil)
  expected_dom, actual_dom = fragment(expected, html_version: html_version), fragment(actual, html_version: html_version)
  message ||= "Expected: #{expected}\nActual: #{actual}"
  assert compare_doms(expected_dom, actual_dom, strict), message
end

#assert_dom_not_equal(expected, actual, message = nil, strict: false, html_version: nil) ⇒ Object Also known as: refute_dom_equal

The negated form of assert_dom_equal.

# assert that the referenced method does not generate the specified HTML string
assert_dom_not_equal(
  '<a href="http://www.example.com">Apples</a>',
  link_to("Oranges", "http://www.example.com"),
)

By default, the matcher will not pay attention to whitespace in text nodes (e.g., spaces and newlines). If you want stricter matching with exact matching for whitespace, pass strict: true:

# these assertions will both pass
assert_dom_equal     "<div>\nfoo\n\</div>", "<div>foo</div>", strict: false
assert_dom_not_equal "<div>\nfoo\n\</div>", "<div>foo</div>", strict: true

The DOMs are 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_not_equal expected, actual, html_version: :html5


68
69
70
71
72
# File 'lib/rails/dom/testing/assertions/dom_assertions.rb', line 68

def assert_dom_not_equal(expected, actual, message = nil, strict: false, html_version: nil)
  expected_dom, actual_dom = fragment(expected, html_version: html_version), fragment(actual, html_version: html_version)
  message ||= "Expected: #{expected}\nActual: #{actual}"
  assert_not compare_doms(expected_dom, actual_dom, strict), message
end