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

Wincent Colaiuta win at wincent.com
Tue May 29 05:36:28 EDT 2007

El 29/5/2007, a las 4:00, David Chelimsky escribió:

> Actually - looking at assert_select (which have_tag and with_tag
> wrap), you should be able to get what you're looking for using the
> nth-child selectors. Check it out:
> http://labnotes.org/svn/public/ruby/rails_plugins/assert_select/ 
> cheat/assert_select.html

Great cheat sheet, that one! Thanks for sharing the link. There's  
also a PDF if anyone wants to print it out:


Yes, I think that would definitely work. If we add that to the list  
of possible techniques we now have three levels of specificity;  
working from least to most specific we have:

1. No ordering requirements, when you only care about the presence of  
tags on the page.

2. Strict ordering requirement, as implemented by the assert_select  
patch that I posted, where you expect tags to appear in the order  
that you assert them in your specs.

3. Ultra-strict positioning, using the nth-child selectors as you  
suggest, where you not only care about the order of the tags but  
specify their numeric position within the list.

Obviously as you move through the list from less to more specific  
your specs become more tightly bound to the structural details of the  
view, it becomes harder to change the view without breaking the  
specs, which in turn means you'll tend to pick up mistakes in the  
view more quickly, but you'll also have a lot more duplication  
between view and spec.

So I guess it's a matter of choosing the right tool for each case.


More information about the rspec-users mailing list