[rspec-users] how to write complex matchers

Yury Kotlyarov yura at brainhouse.ru
Thu Feb 12 06:52:49 EST 2009


Hi,

I am trying to write custom matcher which accepts block to spec views 
with forms/fieldsets/inputs eg

view:
---
<form action='/users'>
  <fieldset>
    <legend>Personal Information</legend>
    <ol>
      <li>
        <label>First name</label>
        <input type="text" name="user[first_name]" />
      </li>
      ...
    </ol>
  </fieldset>
</form>

spec:
---

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

matches? of have_form:
---

    def matches?(response, &block)
      @block = block if block
      response.should @scope.have_tag('form[action=?]', @action) do
        @block.call.matches?(response)
      end
    end matches? of have_field_set: ---

    def matches?(response, &block)

      @block = block if block
      response.should @scope.with_tag('fieldset') do
        @scope.with_tag('legend', @legend) if @legend
        @scope.with_tag('ol') do
          @block.call.matches?(response)
        end
      end
    end but I get: NoMethodError in '/users/new should have form with 
input fields' undefined method `matches?' for true:TrueClass 
./spec/views/users/../../custom_ui_matchers/with_field_set.rb:10:in 
`matches?' 
./spec/views/users/../../custom_ui_matchers/have_form.rb:11:in 
`matches?' 
./spec/views/users/../../custom_ui_matchers/have_form.rb:10:in 
`matches?' ./spec/views/users/new.html.haml_spec.rb:17: 
/usr/lib64/ruby/1.8/timeout.rb:53:in `timeout' something wrong with 
yielding block? Gist: http://gist.github.com/62562 Thanks in advance, Yury






More information about the rspec-users mailing list