SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

    Code:
      def have_a_contact_form &block
        have_a_form_with_id 'contact', &block
      end
    Anyway, when it fails, I get the error:

    Code:
    '/contact/index the contact form before it has been submitted should have a subject dropdown box' FAILED
    expected following output to contain a <select id='subject'/> tag:
    <form action="/contact.html" id="contact" method="post">
      <div>
        <label for="subject">Subject</label>
        <select id="subject" name="subject"><option value=""></option>
    <option value="feedback">Feedback</option>
    <option value="questions">Questions</option>
    <option value="suggestions">Suggestions</option>
    <option value="other">Other</option></select>
    </div>
    </form>
    As you can see, that selector clearly does exist on the page, so it confuses me why it is failing.

    Thanks in advance for any help.

    Brandon
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  2. #2
    SitePoint Addict
    Join Date
    Feb 2007
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just curious: does

    form.should have_selector('select#subject')

    do any better for you?

    It's the "expected" syntax that's bothering me; as a tag "<select id='subject'/>" doesn't make any sense (and no, the tag doesn't exist on the page, at least with that syntax).

  3. #3
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Arlen View Post
    Just curious: does

    form.should have_selector('select#subject')

    do any better for you?

    It's the "expected" syntax that's bothering me; as a tag "<select id='subject'/>" doesn't make any sense (and no, the tag doesn't exist on the page, at least with that syntax).
    No that doesn't change anything.

    I don't think that is the actual string it is looking for, just a description of the selector.

    Do you know what I could be doing wrong? I've been messing with this for days now, and just haven't been able to figure it out.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  4. #4
    SitePoint Addict
    Join Date
    Feb 2007
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm. Is this using the webrat gem? If so, which version?

  5. #5
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Arlen View Post
    Hmmm. Is this using the webrat gem? If so, which version?
    Yes, version 0.4.3 I believe.

    Although I have two versions, so does it by default use the latest?
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  6. #6
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found a gem that a member of the RSpec mailing list wrote, called assert2. It includes assert_xhtml, which goes something like this:

    Code:
          it "should have a subject dropdown box" do
            assert_xhtml do
              form.contact! do
                label 'Subject'
                select!.subject!
              end
            end
          end # it "should have a subject dropdown box"
    That syntax looks a lot nicer than my proposed syntax
    , so I'm switching over to that.

    I'd still like to know why mine wasn't working though.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  7. #7
    SitePoint Addict
    Join Date
    Feb 2007
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The reason I asked was that was a known bug in webrat 0.4.2 (have_selector failed when using a block) not sure if it was fixed in 0.4.3

    You could try unrolling the block, just to test and see if it's the same bug?

  8. #8
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Arlen View Post
    The reason I asked was that was a known bug in webrat 0.4.2 (have_selector failed when using a block) not sure if it was fixed in 0.4.3

    You could try unrolling the block, just to test and see if it's the same bug?
    Interesting. I know I have used have_selector with a block before though, such as:

    Code:
    response.should have_selector('ul li') do |li|
    # ...
    end
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •