Forum: Ruby on Rails functional tests, assert_tag and tables

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.
429e9d8e16b3c6e4ab0c954893e0334a?d=identicon&s=25 mithill (Guest)
on 2005-11-23 03:58
(Received via mailing list)
I've run into another functional testing issue. Odds are I'm taking the
wrong approach, so I'm back to this list again in the hopes of finding
the true path.

with assert_tag, I'd like to check the values of various table cells and
ensure that the proper values are placed in the appropriate places. I
written some tests that make good sense to me, and in general they
behave as expected. I ran into trouble with nested tables. To
demonstrate, I've put together a simple test that calls the index action
of some controller, and this renders a hard-coded html page.

I want to make sure the correct elements are in the "Morning" row, so I
find the "Morning" table cell and verify that it has a parent "tr" tag
that in turn has a child "td" tag that holds the values I want. These
are the first two tests.

For the next test I want to make sure the Teacher's name (Teacher One in
this case) is _not_ put into the cells. When I run these tests against
the html shown, the last test will fail. However, if I remove the
outer-most table (delete or comment out the first 3 lines and the last 3
lines of the html) the tests will all pass.

Am I using assert_tag incorrectly to ensure my tables are constructed
properly? If I am, how should I be traversing a table for values? These
tests seem to make sense, and the :parent appears to be acting as if it
were :ancestor.

************* Begin Functional test ***********************************
def test_general
   get :index
   assert_tag :tag => 'td', :content => 'Morning', :parent => {:tag =>
                                                    'tr', :child =>
{:tag => 'td', :content => 'Subject 1'}}
   assert_tag :tag => 'td', :content => 'Mid-Day', :parent => {:tag =>
'tr', :child => {:tag => 'td', :content => 'Subject 2'}}
   assert_no_tag :tag => 'td', :content => 'Morning', :parent => {:tag
=> 'tr', :child => {:tag => 'td', :content => 'Teacher One'}}
end


*************************** Begin html *************************
<table>
<tr>
<td>
   Schedule for Teacher One
   <table border="1">
     <tr>
       <td>
         Time period
       </td>
       <td align="center">
         November 18, 2005
       </td>
     </tr>
     <tr>
       <td valign="center">
         Morning
       </td>
       <td>
         Subject 1<br/>
       </td>
     </tr>
     <tr>
       <td valign="center">
       Mid-Day
       </td>
       <td>
         Subject 2<br/>
       </td>
     </tr>
     <tr>
       <td valign="center">
       Late
       </td>
       <td>
       </td>
     </tr>
   </table>
</td>
</tr>
</table>
Eb234d1ee9f1dcd334657d7c5c1b1c4d?d=identicon&s=25 Jamie Macey (jamie)
on 2005-11-23 22:29
(Received via mailing list)
On Tue, 2005-11-22 at 19:57 -0700, Matthew Thill wrote:
> I want to make sure the correct elements are in the "Morning" row, so I
> find the "Morning" table cell and verify that it has a parent "tr" tag
> that in turn has a child "td" tag that holds the values I want. These
> are the first two tests.

Matthew,

Instead of chaining through parent to child, try using the :sibling rule
from assert_tag, it should do what you want in both cases.

- Jamie
429e9d8e16b3c6e4ab0c954893e0334a?d=identicon&s=25 mithill (Guest)
on 2005-11-24 17:14
(Received via mailing list)
Thanks Jamie, that was it. It looks like you can't do any sort of
"chaining" with assert_tag statements with reliable results. The
:sibling result should be the same as :parent => {:child =>{}}, or at
least I would think so.

In my defense for not being thorough, the "Guide to Testing the Rails"
how-to I was referring to does not mention :sibling as an option, only
:parent, :child, :children and :ancestor.
This topic is locked and can not be replied to.