| Module | ActionController::Assertions::TagAssertions |
| In: |
vendor/rails/actionpack/lib/action_controller/assertions/tag_assertions.rb
|
Pair of assertions to testing elements in the HTML output of the response.
Identical to assert_tag, but asserts that a matching tag does not exist. (See assert_tag for a full discussion of the syntax.)
# Assert that there is not a "div" containing a "p"
assert_no_tag :tag => "div", :descendant => { :tag => "p" }
# Assert that an unordered list is empty
assert_no_tag :tag => "ul", :descendant => { :tag => "li" }
# Assert that there is not a "p" tag with between 1 to 3 "img" tags
# as immediate children
assert_no_tag :tag => "p",
:children => { :count => 1..3, :only => { :tag => "img" } }
# File vendor/rails/actionpack/lib/action_controller/assertions/tag_assertions.rb, line 121
121: def assert_no_tag(*opts)
122: clean_backtrace do
123: opts = opts.size > 1 ? opts.last.merge({ :tag => opts.first.to_s }) : opts.first
124: tag = find_tag(opts)
125: assert !tag, "expected no tag, but found tag matching #{opts.inspect} in:\n#{@response.body.inspect}"
126: end
127: end
Asserts that there is a tag/node/element in the body of the response that meets all of the given conditions. The conditions parameter must be a hash of any of the following keys (all are optional):
Conditions are matched using the following algorithm:
# Assert that there is a "span" tag
assert_tag :tag => "span"
# Assert that there is a "span" tag with id="x"
assert_tag :tag => "span", :attributes => { :id => "x" }
# Assert that there is a "span" tag using the short-hand
assert_tag :span
# Assert that there is a "span" tag with id="x" using the short-hand
assert_tag :span, :attributes => { :id => "x" }
# Assert that there is a "span" inside of a "div"
assert_tag :tag => "span", :parent => { :tag => "div" }
# Assert that there is a "span" somewhere inside a table
assert_tag :tag => "span", :ancestor => { :tag => "table" }
# Assert that there is a "span" with at least one "em" child
assert_tag :tag => "span", :child => { :tag => "em" }
# Assert that there is a "span" containing a (possibly nested)
# "strong" tag.
assert_tag :tag => "span", :descendant => { :tag => "strong" }
# Assert that there is a "span" containing between 2 and 4 "em" tags
# as immediate children
assert_tag :tag => "span",
:children => { :count => 2..4, :only => { :tag => "em" } }
# Get funky: assert that there is a "div", with an "ul" ancestor
# and an "li" parent (with "class" = "enum"), and containing a
# "span" descendant that contains text matching /hello world/
assert_tag :tag => "div",
:ancestor => { :tag => "ul" },
:parent => { :tag => "li",
:attributes => { :class => "enum" } },
:descendant => { :tag => "span",
:child => /hello world/ }
Please note: assert_tag and assert_no_tag only work with well-formed XHTML. They recognize a few tags as implicitly self-closing (like br and hr and such) but will not work correctly with tags that allow optional closing tags (p, li, td). You must explicitly close all of your tags to use these assertions.
# File vendor/rails/actionpack/lib/action_controller/assertions/tag_assertions.rb, line 99
99: def assert_tag(*opts)
100: clean_backtrace do
101: opts = opts.size > 1 ? opts.last.merge({ :tag => opts.first.to_s }) : opts.first
102: tag = find_tag(opts)
103: assert tag, "expected tag, but no tag found matching #{opts.inspect} in:\n#{@response.body.inspect}"
104: end
105: end