Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #5936 (closed enhancement: fixed)

Opened 3 years ago

Last modified 3 years ago

[PATCH] assert_select for testing views using CSS selectors

Reported by: assaf.arkin@gmail.com Assigned to: David
Priority: normal Milestone:
Component: ActionPack Version:
Severity: normal Keywords:
Cc:

Description

Alternative to assert_tag that uses CSS selectors for a more readable and concise syntax.

Examples:

assert_select “form[action=http://test.host/login]” do
  assert_select “input[name=username]”
  assert_select “input[name=password]”
end
assert_select “ol>li” do
  # List item has an ID we can relate to.
  assert_select “li#?”, /item-d+/
  assert_select “p”
  # And a link to that resource.
  assert_select “a[href=?]”, /item/d+/
end

The patch adds HTML::Selector to the html-scanner library, supporting CSS Level 3 selectors including pseudo classes and substitution values (see example above).

The patch adds the following methods to Test::Unit::TestCase:

* assert_select for -- Assertions on the result of a view or enclosing assertion (for nested assertions).

* css_select -- Selects elements without asserting.

* assert_select_rjs -- Extracts HTML from an RJS update or insert.

* assert_select_feed -- Supports assertions on an RSS or Atom feed.

* assert_select_encoded -- Used for encoded content in RSS/Atom feeds.

* assert_select_email -- For HTML e-mails.

The last four are convenient methods for extracting different types of output. Use assert_select within a block to make assertions on the output.

For more info see Testing with CSS selectors, assert_select plugin for Rails and All posts on assert_select.

Attachments

assert_select_patch.diff.tgz (17.7 kB) - added by assaf.arkin@gmail.com on 08/28/06 15:31:14.
assert_select patch to actionpack
assert_select_text_patch.diff.tgz (2.5 kB) - added by assaf.arkin@gmail.com on 09/12/06 23:17:31.
assert_select text handling patch

Change History

08/28/06 15:31:14 changed by assaf.arkin@gmail.com

  • attachment assert_select_patch.diff.tgz added.

assert_select patch to actionpack

09/03/06 19:54:37 changed by david

  • status changed from new to closed.
  • resolution set to fixed.

This is really, really great stuff. I cut out assert_select_feed, though. That felt too specific to be in the bunch, but the others are great. So good that assert_tag is deprecated straight away. Applied in [4929].

09/11/06 04:28:58 changed by assaf.arkin@gmail.com

Using assert_select with a text value first selects all matching elements based on the CSS selector, and then asserts they all have the same text value. The behavior could be more intuitive. For example:

assert_select "p", "Welcome"

This assertion fails if it finds one paragraph with the text "Welcome", and another paragraph with a different text value.

There's a way around it using the :content pseudo class. But this patch makes the behavior more intuitive and doesn't require trickery.

The patch changes the behavior to select elements using the CSS selector, then narrow them down to match the text value, and finally assert the number of elements found. The above assertions will succeed if at least one paragraph is found with the text value "Welcome".

This patch also adds more pre-formatted text elements (pre, script, style and textarea) and improves the API descripton of the assert_select equality options.

(follow-ups: ↓ 4 ↓ 5 ) 09/11/06 04:34:37 changed by david

did you upload the patch?

(in reply to: ↑ 3 ) 09/11/06 04:41:37 changed by assaf.arkin@gmail.com

Replying to david@loudthinking.com:

did you upload the patch?

Trying to upload the patch but getting error messages from Trac. Failure to create unique name.

(in reply to: ↑ 3 ; follow-up: ↓ 6 ) 09/11/06 04:49:38 changed by anonymous

Replying to david@loudthinking.com:

did you upload the patch?

looks like a setup problem:

http://trac.edgewall.org/ticket/2815

09/12/06 23:17:31 changed by assaf.arkin@gmail.com

  • attachment assert_select_text_patch.diff.tgz added.

assert_select text handling patch

(in reply to: ↑ 5 ) 10/02/06 21:32:10 changed by Assaf

David,

Second patch included as the second attachment. Can you please add it in?

See also ticket #6332.