Forum: RSpec have_tag with a block

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
07dd367bcc4ae602d640ec8c1b280df6?d=identicon&s=25 MarkMT (Guest)
on 2009-02-04 02:56
(Received via mailing list)
This is really a webrat question, but I haven't had any reaction to
this from the webrat group. Maybe someone here can offer a comment...

I've been fooling around with webrat view matchers in merb / cucumber
and I'm trying to figure out whether the block parameter in have_tag
actually does anything. I've tried passing a block with pure nonsense
in it and the interpreter just seems to accept it and ignore it as if
it doesn't exist.

Initially I had hoped there was something like 'with_tag' you could
nest inside a block, but I guess that was a rails thing. However from
my quick look at the code, although with fairly limited ruby
experience, I got the impression that 'have_tag' passes any matching
markup it finds into the block as a parameter. But none of my
experiments have been able to confirm this.

Does anyone have first hand knowledge of / successful experience with
have_tag with a block?
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-02-04 03:03
(Received via mailing list)
On Tue, Feb 3, 2009 at 7:52 PM, MarkMT <mark.thomson@ieee.org> wrote:
> nest inside a block, but I guess that was a rails thing. However from
> my quick look at the code, although with fairly limited ruby
> experience, I got the impression that 'have_tag' passes any matching
> markup it finds into the block as a parameter. But none of my
> experiments have been able to confirm this.
>
> Does anyone have first hand knowledge of / successful experience with
> have_tag with a block?

Not first hand experience, but I'm looking at the specs in webrat:

http://github.com/brynary/webrat/blob/e5ae16367cfd...

If you scroll down to describe "#have_tag", you'll see a couple of
examples. It looks like your instinct is at least close to correct.

Can you post an example of what you're trying to do?
07dd367bcc4ae602d640ec8c1b280df6?d=identicon&s=25 MarkMT (Guest)
on 2009-02-04 06:46
(Received via mailing list)
Thanks David. Those specs are very instructive. I have no idea what I
was doing wrong before, but the behavior I'm seeing now is indeed
consistent with the specs and matches what I had understood from the
code I'd looked at (I suspect it'll stop working as soon as I send
this :-) ).

Actually my situation is slightly more complex than what the specs
cover. I have something like this -

<div id="registration_link">
  New user? Register <a href="/User/register">here</a>
</div>

and I'm testing it with a cucumber step that looks like this -

Then /^the response should contain a link to new user registration$/
do
  @response.should have_tag('div', :id =>'registration_link', :content
=> 'New User? Register ') { |match|
    match.should have_tag('a', :href => '/User/register', :content =>
'here')
  }
end

One of the things I was unsure about was whether I could mix both the
text content and the second tag inside the outer element. Turns out
that this works just fine with the test above. Also, have_tag
('div#registration_link'...) works just as well as have_tag('div', :id
=> 'registration_link'...)

Mark.
Ef0db53920b243d6758c2f6b1306df0d?d=identicon&s=25 Steve Ross (cwd)
on 2009-02-04 20:31
(Received via mailing list)
Hi--

On Feb 3, 2009, at 9:44 PM, MarkMT wrote:

>  New user? Register <a href="/User/register">here</a>
>  }
> end
>
> One of the things I was unsure about was whether I could mix both the
> text content and the second tag inside the outer element. Turns out
> that this works just fine with the test above. Also, have_tag
> ('div#registration_link'...) works just as well as have_tag('div', :id
> => 'registration_link'...)
>

I'm writing a similar kind of scenario and want to make sure I am
scoping my "assertion" (if you will) to the stuff inside the tag. The
idea is that all people to whom this page is addressed are on the
address line (but not necessarily the only thing on the address line.
So the first guess was:

<div id="address-line">
   joe, bob
</div>

recipients.split(/,\s*/).each do |recipient|
   response.should have_tag('div[address-line]', recipient)
end

This doesn't work. Change the expectation to:

   response.should have_tag('div[address-line]', Regexp.new(recipient))

and, predictably, it works. But is that scoped to include only text
that matches the regexp within the div[address-line]?

The code I really wanted to write (phrase stolen from the RSpec Book)
was:

recipients.split(/,\s*/).each do |recipient|
   response.should have_tag('div[address-line]') do |r|
     r.should include(recipient)
   end
end

But that gives me a failed expectation showing a gruesomely
complicated representation of the page DOM and comparing it to 'joe'.

Question: Is there an accepted way to scope text substrings to within
tags?

Thanks
This topic is locked and can not be replied to.