Forum: Ruby on Rails assert_select

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jim B. (Guest)
on 2009-05-20 19:38
Hi,

Testing the view of an application with functional tests, how would one
go about checking that there are a certain number of <span> elements
present on a page, which all belong to a certain class.

I tried:

      assert_select "form" do
         assert_select "span", :class=> "required_field", :count => 13
      end

But this just found all of the span elements on the page and returned
false.

Grateful as ever for any help.
Rick D. (Guest)
on 2009-05-20 20:00
(Received via mailing list)
On Wed, May 20, 2009 at 11:38 AM, Jim B.
<removed_email_address@domain.invalid> wrote:
>         assert_select "span", :class=> "required_field", :count => 13
>      end
>
> But this just found all of the span elements on the page and returned
> false.

You need to use the css selector to specify spans with a particular
class:

assert_select("span.required_field", :count => 13)

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
Phlip (Guest)
on 2009-05-20 20:15
(Received via mailing list)
Jim B. wrote:
>       end
>
> But this just found all of the span elements on the page and returned
> false.

At a guess...

   assert_xhtml do
     form :xpath! => 'count(.//span[ @class = "required_field" ]) = 13'
   end

If that doesn't work, look up the XPath syntax for count(). If that
_still_
doesn't work, throw away assert_xhtml, and drop down raw XPath thru
Nokogiri to
do it.

(gem install rkelly nokogiri assert2, and require 'assert2/xhtml', to
get
assert_xhtml, IIRC..)

--
   Phlip
Jim B. (Guest)
on 2009-05-20 20:30
Thanks for the answers

I just looked again at the html rails is generating and in fact the
element I'm looking for looks like this:

<span title="required field">*</span>

not:

<span class="required_field">*</span>

Would I test for this like so:

      assert_select "form" do
        assert_select("span.title.required field", :count => 13)
      end

When I run it, I get the error message:
Expected at least 13 elements matching "span.required_field", found 0.

I installed the recommended gem and using that the test works just fine:

        assert_xhtml do
          form :xpath! => 'count(.//span[ @title= "required field" ]) =
13'
        end

One question however, is there any way to turn off the "...in this
sample..." aspect of the output, as if the does find any errors, it
returns my entire html page.

E.g.

  1) Failure:
test_index_view(FormControllerTest)
    [c:/ruby/lib/ruby/gems/1.8/gems/assert2-0.5.3/lib/assert2/xhtml.rb:327:in
`assert_xhtml'
     /test/functional/form_controller_test.rb:41:in `test_index_view'
     /test/functional/form_controller_test.rb:37:in `test_index_view']:

Could not find this reference...


<form xpath!="count(.//span[ @class = &quot;required field&quot; ]) =
13"></form>


...in this sample...

300 lines of html follow
Colin L. (Guest)
on 2009-05-20 20:40
(Received via mailing list)
To do what you want with assert_select you can use
assert_select "form>span[title=required field]", 13
That is there should be 13 span elements with given title inside form
elements

or
assert_select "form" do
  assert_select "span[title=required field]", 13
end
if you want to insist that all 13 are in the same form

Colin

2009/5/20 Jim B. <removed_email_address@domain.invalid>
Jim B. (Guest)
on 2009-05-20 20:46
> assert_select "form" do
>   assert_select "span[title=required field]", 13
> end

Excellent, that is exactly what I was after thanks very much.
This topic is locked and can not be replied to.