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

Pat Maddox pat.maddox at gmail.com
Sun Mar 15 15:32:01 EDT 2009

On Mar 15, 2009, at 10:33 AM, Zach Dennis wrote:

> 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
> other).

Both of those points make sense to me.  What about

  response.body.should be_html_with{
     form :action => '/users' do
       fieldset do
         legend 'Personal Information'

         anything do
           label 'First name'
           input :type => 'text', :name => 'user[first_name]'

I dunno.  Zach's original attempt hardly "sucks".  But maybe you want  
to make the structure a bit more explicit?  Personally I think that it  
becomes too coupled to the HTML, and "anything {}" is added noise, but  
maybe that gets you moving in the right direction.


More information about the rspec-users mailing list