As I understand it, assert_select is a Capybara method for testing the state of an HTML <select> element, and assert_selector is for testing the presence of any HTML element and its inner text.

Why then, does:

assert_select "h1", "Foobar"

Work, when h1 is not a <select> element? The Rails Guide seems inconsistent here.

2 Answers

0
Chap Chipley On

you probably want:

assert_selector "h1", text: "Foobar"
0
mechnicov On

It looks like some misunderstanding with terms.

CSS-selectors are patterns used to select the element(s) you want to style. And assert_select is used for selectors testing (not only for <select> elements).

There is the link in your question to Rails Guide. From there:

The assert_select method allows you to query HTML elements of the response by using a simple yet powerful syntax.

There are two forms of assert_select:

assert_select(selector, [equality], [message]) ensures that the equality condition is met on the selected elements through the selector. The selector may be a CSS selector expression (String) or an expression with substitution values.

assert_select(element, selector, [equality], [message]) ensures that the equality condition is met on all the selected elements through the selector starting from the element (instance of Nokogiri::XML::Node or Nokogiri::XML::NodeSet) and its descendants.

For example, you could verify the contents on the title element in your response with:

assert_select 'title', "Welcome to Rails Testing Guide"

You can also use nested assert_select blocks for deeper investigation.

In the following example, the inner assert_select for li.menu_item runs within the collection of elements selected by the outer block:

assert_select 'ul.navigation' do
  assert_select 'li.menu_item'
end