how to test the contents of the p tag

31 views
Skip to first unread message

Roelof Wobben

unread,
Dec 5, 2014, 2:12:36 PM12/5/14
to rubyonra...@googlegroups.com
Hello,

I want to test the contents of the p tag here :

~~~

<div class="productinfo text-center">
                                              <%= image_tag product.image_url %>
                                              <h2><%=number_to_currency(product.price)%></h2>
                                               <p> <%= product.title %>   </p>
              <a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
          </div>

~~~

I tried this assert_select'.single-products.p',1 but it do not work. I get 0 items found .

Roelof

dasibre

unread,
Dec 6, 2014, 8:52:06 AM12/6/14
to rubyonra...@googlegroups.com
The problem with testing to that level is, sometimes the tag may not be rendered yet. Also, such tests tend to be brittle. You dont want to depend on things that change often, I would probably test that the product.title exists, regardless of what tag its wrapped in.

Roelof Wobben

unread,
Dec 6, 2014, 1:09:54 PM12/6/14
to rubyonra...@googlegroups.com
Thanks,

And how do I test this on a functional test for a controller.

Roelof


Op zaterdag 6 december 2014 14:52:06 UTC+1 schreef dasibre:

dasibre

unread,
Dec 9, 2014, 2:09:49 PM12/9/14
to rubyonra...@googlegroups.com
I'm not very familiar with the out of the box Rails Testing framework you are using; I use Rspec. In your current case, it looks like you are getting 0 cause there is not .singleproducts class selector in the code

the p tag is nested inside the <div class="productinfo text-center">.
so you would probably need to access it using .productinfo.p
Reply all
Reply to author
Forward
0 new messages