#170 ✓invalid
Matt Blodgett

have_selector() doesn't work with block

Reported by Matt Blodgett | February 18th, 2009 @ 04:48 PM

Let's say my view contains a snippet like this:

<div class="first_name"> Matt </div>

And my step contains something like this:

response.should have_selector(".first_name") do |scope| puts "=== this code never runs ===" end

The code inside the block is never executed.

Comments and changes to this ticket

  • Matt Blodgett

    Matt Blodgett February 18th, 2009 @ 04:52 PM

    Let's say my view contains a snippet like this:

    <div class="first_name">
      Matt 
    </div>
    
    

    And my step contains something like this:

    response.should have_selector(".first_name") do |scope|
      puts "=== this code never runs ===" 
    end
    
    

    The code inside the block is never executed.

  • Matt Blodgett

    Matt Blodgett February 18th, 2009 @ 05:02 PM

    Whoops. Sorry about the double entry. I was just trying to fix the formatting of the code samples.

  • Simon de Boer

    Simon de Boer March 10th, 2009 @ 09:15 PM

    • Tag changed from bug to bug, have_selector, rspec

    I have a similar problem that is (probably) the same bug.

    HTML Code:

    
    <a href="gndn.html">Going Nowhere</a>
    

    Spec code to find it, maybe:

    
    it "should have a link to nowhere" do
      render "nowhere.html.erb"
      response.should have_selector("a[href]", :content => 'Going Nowhere')
    end
    

    Fails with the following:

    
    ... should have a link to nowhere' FAILED
    expected following output to contain a <a[href]>Going Nowhere</a[href]> tag:
    

    I'm using Webrat version 0.4.2.

  • Simon de Boer

    Simon de Boer March 10th, 2009 @ 09:36 PM

    But...

    If I just do:

    
    it "should have a link to nowhere" do
      render "nowhere.html.erb"
      response.should have_selector("a[href]")
    end
    

    It seems to work (for any link on the page though, not just the one with content I want.)

  • Peter Jaros

    Peter Jaros March 23rd, 2009 @ 04:22 PM

    This should be fixed in 0.4.3. Matt, can you confirm?

  • gaffo

    gaffo May 7th, 2009 @ 06:21 PM

    • State changed from “new” to “open”
  • Francisco Viramontes

    Francisco Viramontes May 16th, 2009 @ 08:10 PM

    I have a similar problem

    with this

    @@@ruby Then /^I should see a list of the jobs filtered by the search query "([^"]*)"$/ do |arg1|
    response.should have_selector("tr.job_row") do |tr|

    tr.should contain(/#{arg1}/)
    

    end end

    
    I get this error


    expected following output to contain a <tr.job_row/> tag:


    css selector is not working or something becaus it wants to find a tag not a class
  • Andrew Vit

    Andrew Vit June 10th, 2009 @ 10:18 AM

    I can confirm this bug is still present in 0.4.4

    I stepped through my code with the debugger, and it looks like the block is nil when have_selector is called. Strangely, this doesn't work:

      response.should have_tag "#product_1" do |item|
        item.should contain("=== this code never runs ===")
      end
    

    But this works, I get the expected failure based on the content block:

      content_expectation = proc do |item|
        item.should contain("=== this code gets called ===")
      end
      response.should have_tag("#product_1", {}, &content_expectation)
    
  • gaffo

    gaffo June 10th, 2009 @ 03:41 PM

    Throwing in a pending spec would be a great first step to getting this done.

    -Mike

  • Andrew Vit

    Andrew Vit June 10th, 2009 @ 07:13 PM

    I had a look and I'm not sure where to start with a spec for this: there's already a spec for have_selector which verifies that an ExpectationNotMetError gets raised when using a block. (have_selector_spec.rb:73)

    For the record, I'm using rspec-rails 1.2.6 with rails 2-3-stable.

  • gaffo

    gaffo June 10th, 2009 @ 07:27 PM

    Assert that your block gets called in the test, or does that other test already do so? For example:

     pass = false
     Have_selector do


    End Assert(pass)

    -- Sent from my Palm Pre Lighthouse wrote:

  • Andrew Vit

    Andrew Vit June 10th, 2009 @ 07:59 PM

    Ok, I think this is a big "never mind"... looks like you can close this ticket. I was running into this problem in a cucumber step, not plain rspec, and I had a custom expectation helper overriding the default should / should_not. That helper was dropping the block parameter on the floor.

    Works for me in 0.4.4. Cheers.

  • gaffo

    gaffo June 10th, 2009 @ 08:14 PM

    • State changed from “open” to “invalid”

    Cool, thanks for being responsive and submitting. [#170 state:invalid]

    -- Sent from my Palm Pre Lighthouse wrote:

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Ruby Acceptance Testing for Web applications.

Shared Ticket Bins

Referenced by

Pages