The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

RSpec Custom Matcher with Block

Hi,

I've been stumped about this for days now. I'm using RSpec for BDD, and tried creating a custom matcher to test for the existence of a form on a page.

The custom matcher is:

Code:

require 'webrat/core/matchers/have_selector'
module FormMatchers
class HaveAFormWithID
include Webrat::Matchers
def initialize id, &block
@id = id
@block = block
end
def matches? response, &block
@block ||= block
response.should have_selector('form#%s' % [@id]) do |form|
!@block or @block.call form
end
end
def description
"have a form with id #{@id}"
end
def failure_message
"expected to have a form with ID #{@id}"
end
def negative_failure_message
"expected not to have a form with ID #{@id}"
end
end
def have_a_form_with_id id, &block
HaveAFormWithID.new id, &block
end
end

An example of one of the examples using it:

Code:

it "should have a subject dropdown box" do
response.should have_a_contact_form do |form|
form.should have_selector('select', :id => 'subject')
end
end # it "should have a subject dropdown box"

That fails. However, if no block is passed, it passes, such as the following:

Code:

it "should show the contact form" do
response.should have_a_contact_form
end

Yes, I have a method have_a_contact_form that calls the have_a_form_with_id method.