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

David Chelimsky dchelimsky at gmail.com
Mon May 28 22:00:34 EDT 2007

On 5/24/07, Wincent Colaiuta <win at wincent.com> wrote:
> El 24/5/2007, a las 16:24, Matt Deiters escribió:
> > A couple of questions/comments. Are you doing some sorting logic in
> > the view? If so I can see why you would want to assert order, but
> > perhaps it would be better to move that type of logic out of view
> > and into your controller (or perhaps a presenter or helper). Then
> > you would test the order of your collection in one of those (helper/
> > controller) types of specs. Then your view would just iterate of
> > the collection and if your view is just iterating over the
> > collection, then when your testing order you would really be
> > testing does 'ruby iterate right', which again is something we
> > don't want to test in our views (or need test at all).
> No, the view has no sorting logic:
> <% for user in @users %>
> <div id="userlist">
>    <div class="<%= cycle('odd', 'even') %>">
>      <div class="label">Login name:</div>
>      <div class="info"><%=h user.login_name %></div>
>      <div class="label">Display name:</div>
>      <div class="info"><%=h user.display_name %></div>
>      <div class="label">Email address:</div>
>      <div class="info"><%=h user.email_address %></div>
>    </div>
> </div>
> <% end %>
> But I do want to be sure of some things, and these stemmed out of
> behaviour-driving the development of the view. I basically imagined
> what I wanted the view to look like in my mind and started coming up
> with specifications:
> - users should be listed in rows
> - rows should have alternate classes (odd, even, odd, even) so they
> can be visually styled
> - there should be three fields for each user
> - the fields should appear in order: login name, display name, email
> address
> - each field should be preceded by the correct, corresponding label
> - users should appear in the view in the order that they were passed
> to it
> - etc
> So when I wrote the view based on the specs. I was dismayed to find
> that if I ignored the question of ordering then I could radically
> change the view and the specs would still pass, even though I the
> view no longer resembled what I wanted it to look like in my mind...
> Basically, I want to be able to assert that "rows should be odd,
> even" and only "odd, even" should pass, not "even, odd". Likewise, I
> want to be able to assert "header, content, footer" and not accept
> results like "footer, header, content" etc.
> In the case where order doesn't matter at all and you only want to
> test for the presence of something on the page, you can easily do
> that with a single "have_tag" inside an example "it" block of its own.

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:


Let us know if it works.

> Cheers,
> Wincent
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list