Assert_select issue

I’d like to do an assert_select where only ONE of the matching elements
needs to match the :text.

It seems counterintuitive to me to require them all to match, which
precludes me from testing the existence of a particular tag (h1) with
some particular text if other instances of the same tag exist.

I know that I can narrow down the field by using more detailed
selectors, but it sure would be nice to be able to optionally use :text
as an additional selector (and then be able to determine the number of
elements matching the selector and containing the text, or matching the
text RegExp).

Any ideas?

Yehuda K. wrote:

Any ideas?

Use assert_select to select the node around the target node you need.
Then do this:

assert_select ‘some node’ do |node|
assert_xml node.to_s
assert_xpath ‘match_me[ . = “contents” ]’

Problem: XPath can’t do RegExps (last time I checked). But it can do
any other kind of query you can think of, competitive with query
systems like SQL SELECT.

The assert_xpath support code is below my sig.

Phlip <-- NOT a blog!!

require ‘rexml/document’
include REXML

module AssertXPath

def assert_xml(contents)
contents.gsub!(’\’’, ‘’’)
@xdoc =

def assert_xpath(path, message = ‘’)
former = @xdoc
@xdoc = XPath.first(@xdoc, path)

assert_not_nil @xdoc, message +
        "\nseeking: #{path.inspect} in\n#{former.to_s}"

yield(@xdoc) if block_given?

@xdoc = former


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs