[rspec-users] custom, nested HTML matchers in RSpec

Zach Dennis zach.dennis at gmail.com
Sun Mar 15 13:33:13 EDT 2009

On Sun, Mar 15, 2009 at 1:05 PM, Phlip <phlip2005 at gmail.com> wrote:
>>>   response.body.should be_html_with{
>>>     form :action => '/users' do
>>>       fieldset do
>>>         legend 'Personal Information'
>>>         label 'First name'
>>>         input :type => 'text', :name => 'user[first_name]'
>>>       end
>>>     end
>>>   }
>> Has anyone tried this? is it useful?
> That assertion sucks! The actual sample HTML sez:
>  <fieldset>
>    <legend>Personal Information</legend>
>    <ol>
>      <li id="control_user_first_name">
>        <label for="user_first_name">First name</label>
>        <input type="text" name="user[first_name]" id="user_first_name" />
>      </li>
>    </ol>
>  </fieldset>
> The <legend> and <label> live in different containers, so they are not peers
> of each other. But the assertion specifies they are!

I didn't have the expectation that they were peers of each other, just
that they both existed somewhere in a fieldset tag. Any helper or
matchers used for spec'ing views should be as liberal as possible
while still communicating enough about the semantics of the page for
the example to be meaningful.

I would not expect nor want a matcher to default to verifying direct
descendant, sibling, or adjacent nodes. Those are best verified by a
person looking at the page. What I do care about is that fields in are
within a fieldset (but not necessarily immediately next to each

> I think the assertion should fault until you specify at least this:
>    response.body.should be_html_with{
>      form :action => '/users' do
>        fieldset do
>          legend 'Personal Information'
>          li do
>            label 'First name'
>            input :type => 'text', :name => 'user[first_name]'
>          end
>        end
>      end
>    }

I don't like this, we are specifying the "li" but that provides no
meaning of the page, it's strictly markup for presentation.

> Notice we have some conflicting requirements:
>  - you can skip any tag
>  - the reference should "look like" the sample (except it's in Ruby)
> But you should not be able to skip a tag if its absence makes the reference
> "look different" from the sample. Hence, the test should require at least
> one of the <ol><li> tags that separate the two contexts...
> (BTW the common verbiage for chemical tests of analytes here is "sample" for
> the substance submitted for analysis, and "reference" for the known sample
> used to calibrate the test.)
> --
>  Phlip
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

Zach Dennis

More information about the rspec-users mailing list