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

David Chelimsky dchelimsky at gmail.com
Mon May 21 20:01:23 EDT 2007

On 5/21/07, Wincent Colaiuta <win at wincent.com> wrote:
> 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')
>      end
>      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')
>      end
>    end
> 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.

Not to pass the buck ... but I'm going to pass the buck on this one.
have_tag and with_tag wrap assert_select, which ships with Rails. I've
actually submitted a couple of patches to assert_select to Rails
myself because I'd like to see better error messages and more rjs
coverage. I'd encourage you to make this request and/or submit a patch
to Rails directly as well.


> 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