[rspec-users] how to write complex matchers

Phlip phlip2005 at gmail.com
Fri Feb 13 14:53:44 EST 2009


Yury Kotlyarov wrote:

> it "should have form with input fields" do
>  render ...
>  response.should have_form(users_path) do
>    with_field_set 'Personal Information' do
>      with_text_field 'First name', 'user[first_name]'
>      ...
>    end
>  end
> end

The minor problem with that system is it forces your test to say exactly what 
the code says. That's not "driven" development!

If you can forbear to use matchers (shocked gasp!), at my day-job we match 
blocks all the time with assert2's new xpath system:

   require 'assert2/xpath'

   assert_xhtml response

   xpath :form, :action => users_path do
     xpath :fieldset, ?. => 'Personal Information' do
       xpath :input, :type => 'text', :name => 'user[first_name]' and
       xpath :input, :type => 'text', :name => 'user[last_name]'
     end
   end

 From there, wrapping the xpath() calls up into kewt with_text_field() macros 
would be trivial. They could also absolves the redundant 'user[]' text on the 
names, for example.

If any inner xpath() fails, there, the fault diagnostic contains a formatted & 
indented copy of the HTML block under inspection. The entire page would not spew 
out! Only the <form> or <fieldset> would.

-- 
   Phlip



More information about the rspec-users mailing list