assert_select
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0 (0)
- 3.0.9 (-2)
- 3.1.0 (0)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (0)
- 4.1.8 (0)
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
assert_select(*args, &block)
public
An assertion that selects elements and makes one or more equality tests.
If the first argument is an element, selects all matching elements starting from (and including) that element and all its children in depth-first order.
If no element if specified, calling assert_select selects from the response HTML unless assert_select is called from within an assert_select block.
When called with a block assert_select passes an array of selected elements to the block. Calling assert_select from the block, with no element specified, runs the assertion on the complete set of elements selected by the enclosing assertion. Alternatively the array may be iterated through so that assert_select can be called separately for each element.
Example
If the response contains two ordered lists, each with four list elements then:
assert_select "ol" do |elements| elements.each do |element| assert_select element, "li", 4 end end
will pass, as will:
assert_select "ol" do assert_select "li", 8 end
The selector may be a CSS selector expression (String), an expression with substitution values, or an HTML::Selector object.
Equality Tests
The equality test may be one of the following:
-
true - Assertion is true if at least one element selected.
-
false - Assertion is true if no element selected.
-
String/Regexp - Assertion is true if the text value of at least one element matches the string or regular expression.
-
Integer - Assertion is true if exactly that number of elements are selected.
-
Range - Assertion is true if the number of selected elements fit the range.
If no equality test specified, the assertion is true if at least one element selected.
To perform more than one equality tests, use a hash with the following keys:
-
:text - Narrow the selection to elements that have this text value (string or regexp).
-
:html - Narrow the selection to elements that have this HTML content (string or regexp).
-
:count - Assertion is true if the number of selected elements is equal to this value.
-
:minimum - Assertion is true if the number of selected elements is at least this value.
-
:maximum - Assertion is true if the number of selected elements is at most this value.
If the method is called with a block, once all equality tests are evaluated the block is called with an array of all matched elements.
Examples
# At least one form element assert_select "form" # Form element includes four input fields assert_select "form input", 4 # Page title is "Welcome" assert_select "title", "Welcome" # Page title is "Welcome" and there is only one title element assert_select "title", {:count=>1, :text=>"Welcome"}, "Wrong title or more than one title element" # Page contains no forms assert_select "form", false, "This page must contain no forms" # Test the content and style assert_select "body div.header ul.menu" # Use substitution values assert_select "ol>li#?", /item-\d+/ # All input fields in the form have a name assert_select "form input" do assert_select "[name=?]", /.+/ # Not empty end
Use @output_buffer to set the context.
You can use assert_select to test helpers, just have to set the @output_buffer before you do.
Code example
class CurrencyHelperTest < ActionView::TestCase setup do # can use helper methods here @output_buffer = currency 54.78 end test 'currency use a div' do asert_select 'div' end end