Module | ActionController::Assertions::TagAssertions |
In: |
vendor/rails/actionpack/lib/action_controller/assertions/tag_assertions.rb
|
Identical to assert_tag, but asserts that a matching tag does not exist. (See assert_tag for a full discussion of the syntax.)
# File vendor/rails/actionpack/lib/action_controller/assertions/tag_assertions.rb, line 108 108: def assert_no_tag(*opts) 109: clean_backtrace do 110: opts = opts.size > 1 ? opts.last.merge({ :tag => opts.first.to_s }) : opts.first 111: tag = find_tag(opts) 112: assert !tag, "expected no tag, but found tag matching #{opts.inspect} in:\n#{@response.body.inspect}" 113: end 114: 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):
given value. This will not match HTML tags in the body of a tag--only text.
Conditions are matched using the following algorithm:
Usage:
# 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/ }
<strong>Please note</strong: 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 98 98: def assert_tag(*opts) 99: clean_backtrace do 100: opts = opts.size > 1 ? opts.last.merge({ :tag => opts.first.to_s }) : opts.first 101: tag = find_tag(opts) 102: assert tag, "expected tag, but no tag found matching #{opts.inspect} in:\n#{@response.body.inspect}" 103: end 104: end