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

Phlip phlip2005 at gmail.com
Sun Mar 15 13:05:03 EDT 2009


>>    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 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
     }

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



More information about the rspec-users mailing list