[rspec-users] Ordering in view specs using have_tag and with_tag

Wincent Colaiuta win at wincent.com
Mon May 21 17:18:16 EDT 2007

When writing view specs is there any way to test not only for the  
presence of tags (have_tag) and nested tags (with_tag), but also test  
that they appear in a given order?

For example, consider the following:

   it 'should display the login names, display names and email  
address in alternating rows' do
     response.should have_tag('div.odd>div') do
       with_tag('div', 'example login name 1')
       with_tag('div', 'example display name 1')
       with_tag('div', 'example email address 1')
     response.should have_tag('div.even>div') do
       with_tag('div', 'example login name 2')
       with_tag('div', 'example display name 2')
       with_tag('div', 'example email address 2')

I can change the order of the have_tag blocks and I can also change  
the order of the with_tag calls within each block without breaking  
the specs. Or to put it alternatively, my view can render the "odd"  
and "even" divs in a different order, or the nested divs inside each,  
without breaking the specs.

In many cases, this might not matter. But what if I want to test for  
alternating divs (odd, even, odd even)? I can add multiple have_tag  
assertions but their order won't be considered, so I'm not really  
testing the alternation, only for existence.


More information about the rspec-users mailing list