Functional tests, assert_tag and tables

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 *************************

Schedule for Teacher One
Time period November 18, 2005
Morning Subject 1
Mid-Day Subject 2
Late

On Tue, 2005-11-22 at 19:57 -0700, Matthew T. 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

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.