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

Phlip phlip2005 at gmail.com
Mon Mar 9 02:59:14 EDT 2009

Zach Dennis wrote:

 >> response.body.should be_xml_with do
 >>  form :action => '/users' do
 >>    fieldset do
 >>      legend "Personal Information"
 >>      label "First name"
 >>      input :type => 'text', :name => 'user[first_name]'
 >>    end
 >>  end
 >> end

 > I like this a lot.

Boom: http://gist.github.com/76136

   it 'should have a form with a fieldset' do
     render '/users/new'

     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]'

I think I never got around to writing that because I just always assumed someone 
already had. I have to watch that about myself!

Only one feature is missing: At fault time, the matcher naturally prints out a 
diagnostic containing two snips of HTML - the specification's reference, and 
your page's sample. (Both, of course, are restricted to the fault's context - 
not the whole page.) But...

The samples are not indented! The specification's reference is all run together, 
and your page's sample is (unfortunately!) exactly the way your View system 
generated it. I have a question out to Nokogiri about this...


More information about the rspec-users mailing list