Module: ActionDispatch::Assertions::TagAssertions

### Home Classes Methods

### In Files

- lib/action_dispatch/testing/assertions/tag.rb

### Methods

- #assert_no_tag
- #assert_tag
- #find_all_tag
- #find_tag
- #html_document

### Class/Module Index

Quicksearch

- ActionDispatch
- ActionDispatch::Assertions
- ActionDispatch::Assertions::DomAssertions
- ActionDispatch::Assertions::ResponseAssertions
- ActionDispatch::Assertions::RoutingAssertions
- ActionDispatch::Assertions::SelectorAssertions
- ActionDispatch::Assertions::TagAssertions
- ActionDispatch::BestStandardsSupport
- ActionDispatch::BodyProxy
- ActionDispatch::Callbacks
- ActionDispatch::Cookies
- ActionDispatch::Cookies::CookieOverflow
- ActionDispatch::DebugExceptions
- ActionDispatch::ExceptionWrapper
- ActionDispatch::FileHandler
- ActionDispatch::Flash
- ActionDispatch::Flash::FlashHash
- ActionDispatch::Head
- ActionDispatch::Http
- ActionDispatch::Http::Cache
- ActionDispatch::Http::Cache::Request
- ActionDispatch::Http::Cache::Response
- ActionDispatch::Http::FilterParameters
- ActionDispatch::Http::Headers
- ActionDispatch::Http::MimeNegotiation
- ActionDispatch::Http::ParameterFilter
- ActionDispatch::Http::Parameters
- ActionDispatch::Http::URL
- ActionDispatch::Http::Upload
- ActionDispatch::Http::UploadedFile
- ActionDispatch::Integration
- ActionDispatch::Integration::RequestHelpers
- ActionDispatch::Integration::Runner
- ActionDispatch::Integration::Session
- ActionDispatch::IntegrationTest
- ActionDispatch::MiddlewareStack
- ActionDispatch::MiddlewareStack::Middleware
- ActionDispatch::ParamsParser
- ActionDispatch::PerformanceTest
- ActionDispatch::PublicExceptions
- ActionDispatch::RailsEntityStore
- ActionDispatch::RailsMetaStore
- ActionDispatch::Railtie
- ActionDispatch::Reloader
- ActionDispatch::RemoteIp
- ActionDispatch::RemoteIp::GetIp
- ActionDispatch::RemoteIp::IpSpoofAttackError
- ActionDispatch::Request
- ActionDispatch::RequestId
- ActionDispatch::Rescue
- ActionDispatch::Response
- ActionDispatch::Routing
- ActionDispatch::Routing::Mapper
- ActionDispatch::Routing::Mapper::Base
- ActionDispatch::Routing::Mapper::HttpHelpers
- ActionDispatch::Routing::Mapper::Resources
- ActionDispatch::Routing::Mapper::Scoping
- ActionDispatch::Routing::PolymorphicRoutes
- ActionDispatch::Routing::Redirection
- ActionDispatch::Routing::RouteSet
- ActionDispatch::Routing::RouteSet::MountedHelpers
- ActionDispatch::Routing::UrlFor
- ActionDispatch::Session
- ActionDispatch::Session::AbstractStore
- ActionDispatch::Session::CacheStore
- ActionDispatch::Session::Compatibility
- ActionDispatch::Session::CookieStore
- ActionDispatch::Session::DestroyableSession
- ActionDispatch::Session::MemCacheStore
- ActionDispatch::Session::StaleSessionCheck
- ActionDispatch::ShowExceptions
- ActionDispatch::Static
- ActionDispatch::TestProcess
- ActionDispatch::TestRequest
- ActionDispatch::TestResponse
- ActionController
- ActionController::Base
- ActionController::Caching
- ActionController::Caching::Actions
- ActionController::Caching::Actions::ActionCachePath
- ActionController::Caching::Actions::ClassMethods
- ActionController::Caching::ConfigMethods
- ActionController::Caching::Fragments
- ActionController::Caching::Pages
- ActionController::Caching::Pages::ClassMethods
- ActionController::Caching::Sweeping
- ActionController::Compatibility
- ActionController::ConditionalGet
- ActionController::Cookies
- ActionController::DataStreaming
- ActionController::Flash
- ActionController::ForceSSL
- ActionController::ForceSSL::ClassMethods
- ActionController::Head
- ActionController::Helpers
- ActionController::Helpers::ClassMethods
- ActionController::HideActions
- ActionController::HideActions::ClassMethods
- ActionController::HttpAuthentication
- ActionController::HttpAuthentication::Basic
- ActionController::HttpAuthentication::Basic::ControllerMethods
- ActionController::HttpAuthentication::Basic::ControllerMethods::ClassMethods
- ActionController::HttpAuthentication::Digest
- ActionController::HttpAuthentication::Digest::ControllerMethods
- ActionController::HttpAuthentication::Token
- ActionController::HttpAuthentication::Token::ControllerMethods
- ActionController::ImplicitRender
- ActionController::Instrumentation
- ActionController::Instrumentation::ClassMethods
- ActionController::LogSubscriber
- ActionController::Metal
- ActionController::Middleware
- ActionController::Middleware::ActionMiddleware
- ActionController::MimeResponds
- ActionController::MimeResponds::ClassMethods
- ActionController::ParamsWrapper
- ActionController::ParamsWrapper::ClassMethods
- ActionController::RackDelegation
- ActionController::Railtie
- ActionController::Railties
- ActionController::Railties::Paths
- ActionController::RecordIdentifier
- ActionController::Redirecting
- ActionController::Renderers
- ActionController::Renderers::All
- ActionController::Renderers::ClassMethods
- ActionController::Rendering
- ActionController::RequestForgeryProtection
- ActionController::RequestForgeryProtection::ClassMethods
- ActionController::Rescue
- ActionController::Responder
- ActionController::SessionManagement
- ActionController::SessionManagement::ClassMethods
- ActionController::Streaming
- ActionController::TemplateAssertions
- ActionController::TestCase
- ActionController::TestCase::Behavior
- ActionController::TestCase::Behavior::ClassMethods
- ActionController::TestCase::RaiseActionExceptions
- ActionController::TestResponse
- ActionController::Testing
- ActionController::Testing::ClassMethods
- ActionController::UrlFor
- ActionView
- ActionView::Base
- ActionView::Context
- ActionView::FileSystemResolver
- ActionView::FixtureResolver
- ActionView::Helpers
- ActionView::Helpers::ActiveModelHelper
- ActionView::Helpers::ActiveModelInstanceTag
- ActionView::Helpers::AssetPaths
- ActionView::Helpers::AssetTagHelper
- ActionView::Helpers::AssetTagHelper::AssetIncludeTag
- ActionView::Helpers::AssetTagHelper::JavascriptIncludeTag
- ActionView::Helpers::AssetTagHelper::JavascriptTagHelpers
- ActionView::Helpers::AssetTagHelper::JavascriptTagHelpers::ClassMethods
- ActionView::Helpers::AssetTagHelper::StylesheetIncludeTag
- ActionView::Helpers::AssetTagHelper::StylesheetTagHelpers
- ActionView::Helpers::AssetTagHelper::StylesheetTagHelpers::ClassMethods
- ActionView::Helpers::AtomFeedHelper
- ActionView::Helpers::AtomFeedHelper::AtomBuilder
- ActionView::Helpers::AtomFeedHelper::AtomFeedBuilder
- ActionView::Helpers::CacheHelper
- ActionView::Helpers::CaptureHelper
- ActionView::Helpers::CsrfHelper
- ActionView::Helpers::DateHelper
- ActionView::Helpers::DateHelperInstanceTag
- ActionView::Helpers::DebugHelper
- ActionView::Helpers::FormBuilder
- ActionView::Helpers::FormHelper
- ActionView::Helpers::FormOptionsHelper
- ActionView::Helpers::FormTagHelper
- ActionView::Helpers::InstanceTag
- ActionView::Helpers::JavaScriptHelper
- ActionView::Helpers::OutputSafetyHelper
- ActionView::Helpers::RecordTagHelper
- ActionView::Helpers::RenderingHelper
- ActionView::Helpers::SanitizeHelper
- ActionView::Helpers::TagHelper
- ActionView::Helpers::TextHelper
- ActionView::Helpers::TranslationHelper
- ActionView::Helpers::UrlHelper
- ActionView::LogSubscriber
- ActionView::LookupContext
- ActionView::LookupContext::DetailsCache
- ActionView::LookupContext::ViewPaths
- ActionView::NullResolver
- ActionView::PartialRenderer
- ActionView::Railtie
- ActionView::Renderer
- ActionView::Resolver
- ActionView::Resolver::Path
- ActionView::Template
- ActionView::Template::Handlers
- ActionView::Template::Handlers::Builder
- ActionView::Template::Handlers::ERB
- ActionView::Template::Handlers::Erubis
- ActionView::TestCase
- ActionView::TestCase::Behavior
- ActionView::TestCase::Behavior::ClassMethods
- ActionView::TestCase::Behavior::Locals
- ActionView::TestCase::TestController
- AbstractController
- AbstractController::ActionNotFound
- AbstractController::AssetPaths
- AbstractController::Base
- AbstractController::Callbacks
- AbstractController::Callbacks::ClassMethods
- AbstractController::Collector
- AbstractController::DoubleRenderError
- AbstractController::Error
- AbstractController::Helpers
- AbstractController::Helpers::ClassMethods
- AbstractController::Layouts
- AbstractController::Layouts::ClassMethods
- AbstractController::Layouts::ClassMethods::LayoutConditions
- AbstractController::Logger
- AbstractController::Railties
- AbstractController::Railties::RoutesHelpers
- AbstractController::Rendering
- AbstractController::Rendering::ClassMethods
- AbstractController::Translation
- AbstractController::UrlFor
- AbstractController::UrlFor::ClassMethods
- AbstractController::ViewPaths
- AbstractController::ViewPaths::ClassMethods
- Sprockets
- Sprockets::Bootstrap
- Sprockets::Compressors
- Sprockets::Helpers
- Sprockets::Helpers::IsolatedHelper
- Sprockets::Helpers::RailsHelper
- Sprockets::Helpers::RailsHelper::AssetPaths
- Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError
- Sprockets::Railtie
- Sprockets::StaticCompiler
- HTML
- HTML::FullSanitizer
- HTML::LinkSanitizer
- HTML::Sanitizer
- HTML::Selector
- HTML::Tag
- HTML::WhiteListSanitizer
- Mime
- Mime::Mimes
- Mime::Type
- ActionPack
- Object
- Rack

No matching classes.

# ActionDispatch::Assertions::TagAssertions

Pair of assertions to testing elements in the HTML output of the response.

### Public Instance Methods

assert_no_tag(*opts) click to toggle source

Identical to `assert_tag`, but asserts that a matching tag does not exist. (See `assert_tag` for a full discussion of the syntax.)

### Examples

    # 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 lib/action_dispatch/testing/assertions/tag.rb, line 118
    def assert_no_tag(*opts)
      opts = opts.size > 1 ? opts.last.merge({ :tag => opts.first.to_s }) : opts.first
      tag = find_tag(opts)
      assert !tag, "expected no tag, but found tag matching #{opts.inspect} in:\n#{@response.body.inspect}"
    end

assert_tag(*opts) click to toggle source

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):

- `:tag`: the node type must match the corresponding value

- `:attributes`: a hash. The node's attributes must match the corresponding values in the hash.

- `:parent`: a hash. The node's parent must match the corresponding hash.

- `:child`: a hash. At least one of the node's immediate children must meet the criteria described by the hash.

- `:ancestor`: a hash. At least one of the node's ancestors must meet the criteria described by the hash.

- `:descendant`: a hash. At least one of the node's descendants must meet the criteria described by the hash.

- `:sibling`: a hash. At least one of the node's siblings must meet the criteria described by the hash.

- `:after`: a hash. The node must be after any sibling meeting the criteria described by the hash, and at least one sibling must match.

- `:before`: a hash. The node must be before any sibling meeting the criteria described by the hash, and at least one sibling must match.

- `:children`: a hash, for counting children of a node. Accepts the keys:

  - `:count`: either a number or a range which must equal (or include) the number of children that match.

  - `:less_than`: the number of matching children must be less than this number.

  - `:greater_than`: the number of matching children must be greater than this number.

  - `:only`: another hash consisting of the keys to use to match on the children, and only matching children will be counted.

- `:content`: the textual content of the node must match the given value. This will not match HTML tags in the body of a tag--only text.

Conditions are matched using the following algorithm:

- if the condition is a string, it must be a substring of the value.

- if the condition is a regexp, it must match the value.

- if the condition is a number, the value must match number.to_s.

- if the condition is `true`, the value must not be `nil`.

- if the condition is `false` or `nil`, the value must be `nil`.

### Examples

    # 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 lib/action_dispatch/testing/assertions/tag.rb, line 98
    def assert_tag(*opts)
      opts = opts.size > 1 ? opts.last.merge({ :tag => opts.first.to_s }) : opts.first
      tag = find_tag(opts)
      assert tag, "expected tag, but no tag found matching #{opts.inspect} in:\n#{@response.body.inspect}"
    end

find_all_tag(conditions) click to toggle source

    # File lib/action_dispatch/testing/assertions/tag.rb, line 128
    def find_all_tag(conditions)
      html_document.find_all(conditions)
    end

find_tag(conditions) click to toggle source

    # File lib/action_dispatch/testing/assertions/tag.rb, line 124
    def find_tag(conditions)
      html_document.find(conditions)
    end

html_document() click to toggle source

    # File lib/action_dispatch/testing/assertions/tag.rb, line 132
    def html_document
      xml = @response.content_type =~ /xml$/
      @html_document ||= HTML::Document.new(@response.body, false, xml)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.